Skip to content

add ndarrays.flags.f_contiguous #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 23, 2023
Merged
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
17 changes: 16 additions & 1 deletion torch_np/_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,22 @@ def base(self):
@property
def flags(self):
# Note contiguous in torch is assumed C-style
return Flags({"C_CONTIGUOUS": self._tensor.is_contiguous()})

# check if F contiguous
from itertools import accumulate

f_strides = tuple(accumulate(list(self._tensor.shape), func=lambda x, y: x * y))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is of course just torch.cumprod(self._tensor.shape, 0); my instinct from numpy usage is to avoid array operations for small tuples etc.

f_strides = (1,) + f_strides[:-1]
is_f_contiguous = f_strides == self._tensor.stride()

return Flags(
{
"C_CONTIGUOUS": self._tensor.is_contiguous(),
"F_CONTIGUOUS": is_f_contiguous,
"OWNDATA": self._tensor._base is None,
"WRITEABLE": True, # pytorch does not have readonly tensors
}
)

@property
def T(self):
Expand Down
1 change: 0 additions & 1 deletion torch_np/tests/numpy_tests/core/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,6 @@ def test_subclass_writeable(self):
assert_(d[...].flags.writeable)
assert_(d[0].flags.writeable)

@pytest.mark.xfail(reason="can't determine f-style contiguous in torch")
def test_memory_order(self):
# This is not necessary to preserve. Memory layouts for
# more complex indices are not as simple.
Expand Down
2 changes: 1 addition & 1 deletion torch_np/tests/numpy_tests/core/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -2194,7 +2194,7 @@ def check_function(self, func, fill_value=None):
**fill_kwarg)

assert_equal(arr.dtype, dtype)
# assert_(getattr(arr.flags, self.orders[order])) # XXX: no ndarray.flags
assert_(getattr(arr.flags, self.orders[order]))

if fill_value is not None:
val = fill_value
Expand Down
2 changes: 1 addition & 1 deletion torch_np/tests/numpy_tests/lib/test_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def test_basic(self):
assert_equal(a[0, 0], 1)
assert_equal(a_copy[0, 0], 10)

@pytest.mark.xfail(reason="ndarray.flags not implemented")
@pytest.mark.xfail(reason="order='F' not implemented")
def test_order(self):
# It turns out that people rely on np.copy() preserving order by
# default; changing this broke scikit-learn:
Expand Down