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
14 changes: 14 additions & 0 deletions test/test_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7310,6 +7310,20 @@ def test_ctor_with_numpy_array(self):
for i in range(len(array)):
self.assertEqual(tensor[i], array[i])

@unittest.skipIf(not TEST_NUMPY, "Numpy not found")
def test_ctor_with_numpy_scalar_ctor(self):
dtypes = [
np.double,
np.float,
np.float16,
np.int64,
np.int32,
np.int16,
np.uint8
]
for dtype in dtypes:
self.assertEqual(dtype(42), torch.tensor(dtype(42)).item())

@unittest.skipIf(not TEST_NUMPY, "Numpy not found")
def test_numpy_index(self):
i = np.int32([0, 1, 2])
Expand Down
6 changes: 4 additions & 2 deletions torch/csrc/utils/tensor_new.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,10 @@ ScalarType infer_scalar_type(PyObject *obj) {
}
#ifdef USE_NUMPY
if (PyArray_Check(obj)) {
auto array = (PyArrayObject*)obj;
return numpy_dtype_to_aten(PyArray_TYPE(array));
return numpy_dtype_to_aten(PyArray_TYPE((PyArrayObject*)obj));
}
if (PyArray_CheckScalar(obj)) {
return numpy_dtype_to_aten(PyArray_TYPE((PyArrayObject*)(PyArray_FromScalar(obj, NULL))));
}
#endif
if (PySequence_Check(obj)) {
Expand Down