Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions test/test_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -11722,6 +11722,19 @@ def test_T(self):
scalar = torch.tensor(5)
self.assertEqual(scalar, scalar.T)

def test_python_types(self):
a1 = torch.randn((1, 2), dtype=torch.float64)
a2 = torch.randn((1, 2), dtype=float)
self.assertEqual(a1.dtype, a2.dtype)

b1 = torch.arange(10, 20, dtype=torch.int64)
b2 = torch.arange(10, 20, dtype=int)
self.assertEqual(b1.dtype, b2.dtype)

c1 = torch.tensor([True, False], dtype=torch.bool)
c2 = torch.tensor([True, False], dtype=bool)
self.assertEqual(c1.dtype, c2.dtype)

# Functions to test negative dimension wrapping
METHOD = 1
INPLACE_METHOD = 2
Expand Down
9 changes: 9 additions & 0 deletions torch/csrc/Dtype.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ inline bool THPDtype_Check(PyObject *obj) {
return Py_TYPE(obj) == &THPDtypeType;
}

inline bool THPPythonScalarType_Check(PyObject *obj) {
return obj == (PyObject*)(&PyFloat_Type) ||
obj == (PyObject*)(&PyBool_Type) ||
#if PY_MAJOR_VERSION == 2
obj == (PyObject*)(&PyInt_Type) ||
#endif
obj == (PyObject*)(&PyLong_Type);
}

PyObject * THPDtype_New(at::ScalarType scalar_type, const std::string& name);

void THPDtype_init(PyObject *module);
2 changes: 1 addition & 1 deletion torch/csrc/utils/python_arg_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ bool FunctionParameter::check(PyObject* obj) {
case ParameterType::BOOL: return PyBool_Check(obj);
case ParameterType::STORAGE: return isStorage(obj);
case ParameterType::PYOBJECT: return true;
case ParameterType::SCALARTYPE: return THPDtype_Check(obj);
case ParameterType::SCALARTYPE: return THPDtype_Check(obj) || THPPythonScalarType_Check(obj);
case ParameterType::LAYOUT: return THPLayout_Check(obj);
case ParameterType::MEMORY_FORMAT: return THPMemoryFormat_Check(obj);
case ParameterType::DEVICE:
Expand Down
16 changes: 15 additions & 1 deletion torch/csrc/utils/python_arg_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,21 @@ inline at::ScalarType PythonArgs::scalartype(int i) {
return (scalartype == at::ScalarType::Undefined) ?
torch::tensors::get_default_scalar_type() : scalartype;
}
return reinterpret_cast<THPDtype*>(args[i])->scalar_type;
PyObject *obj = args[i];
if (obj == (PyObject*)&PyFloat_Type) {
return at::ScalarType::Double;
}
if (obj == (PyObject*)&PyBool_Type) {
return at::ScalarType::Bool;
}
if (obj == (PyObject*)&PyLong_Type
#if PY_MAJOR_VERSION == 2
|| obj == (PyObject*)&PyInt_Type
#endif
) {
return at::ScalarType::Long;
}
return reinterpret_cast<THPDtype*>(obj)->scalar_type;
}

inline c10::optional<at::ScalarType> PythonArgs::scalartypeOptional(int i) {
Expand Down