Skip to content

Commit

Permalink
Make SymFloat behave symmetrically with float in torch.tensor (#109513)
Browse files Browse the repository at this point in the history
Previously, SymFloat would force double precision.  That's wrong;
instead, we must respect default dtype.

Signed-off-by: Edward Z. Yang <ezyang@meta.com>
Pull Request resolved: #109513
Approved by: https://github.com/voznesenskym
  • Loading branch information
ezyang authored and pytorchmergebot committed Sep 19, 2023
1 parent e8ab8c8 commit 2c1554a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
17 changes: 17 additions & 0 deletions test/test_proxy_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,23 @@ def forward(self, a_1):
mul = torch.ops.aten.mul.Tensor(a_1, _local_scalar_dense); a_1 = _local_scalar_dense = None
return mul""")

def test_tensor_symfloat(self):
def f(a):
r = torch.tensor(a.size(0) ** 2.0)
assert r.dtype is torch.float
return r

gm = make_fx(f, tracing_mode="symbolic")(torch.randn(2))
r = str(gm.code).strip()
# NB: this specializes, which is fine, the point is to make sure the
# dtype inference is correct
self.assertExpectedInline(r, """\
def forward(self, a_1):
_tensor_constant0 = self._tensor_constant0
lift_fresh_copy = torch.ops.aten.lift_fresh_copy.default(_tensor_constant0); _tensor_constant0 = None
return lift_fresh_copy""")
self.assertEqual(gm._tensor_constant0, torch.tensor(4.0))

def test_item_to_constructor(self):
def f(a):
r = a.item()
Expand Down
12 changes: 10 additions & 2 deletions torch/csrc/utils/tensor_new.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ ScalarType infer_scalar_type(PyObject* obj) {
return ScalarType::Long;
}
if (torch::is_symfloat(obj)) {
return ScalarType::Double;
return torch::tensors::get_default_scalar_type();
}
#ifdef USE_NUMPY
if (is_numpy_available()) {
Expand Down Expand Up @@ -211,7 +211,15 @@ void recursive_store(
if (is_symfloat) {
auto new_obj = py::reinterpret_borrow<py::object>(obj);
auto val = new_obj.cast<c10::SymFloat>();
*(double*)data = val.guard_float(__FILE__, __LINE__);
const double double_val = val.guard_float(__FILE__, __LINE__);
switch (elementSize) {
case 8:
*reinterpret_cast<double*>(data) = double_val;
break;
case 4:
*reinterpret_cast<float*>(data) = static_cast<float>(double_val);
break;
}
return;
}
if (is_symint) {
Expand Down

0 comments on commit 2c1554a

Please sign in to comment.