-
Notifications
You must be signed in to change notification settings - Fork 4
implement more numpy functions #104
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
Conversation
|
||
@pytest.mark.xfail(reason="how to find if someone is refencing an array") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This we can just do from C++. I don't think we can do this from Python.
@normalizer | ||
def resize(a: ArrayLike, new_shape=None): | ||
|
||
if new_shape is None: | ||
return a | ||
|
||
if a.numel() == 0: | ||
a = torch.zeros(1, dtype=a.dtype) | ||
|
||
numel = math.prod(new_shape) | ||
quot, rem = divmod(numel, a.numel()) | ||
repeats = quot * a.numel() | ||
|
||
result = torch.empty(numel, dtype=a.dtype) | ||
result[:repeats] = torch.tile(a.ravel(), (quot,)) | ||
if rem > 0: | ||
result[-rem:] = a.ravel()[:rem] | ||
return result.reshape(new_shape) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is implemented in Python here. Why not vendor its implementation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup, done in gh-106
@@ -4549,6 +4550,7 @@ def test_none_shape(self): | |||
x.resize() | |||
assert_array_equal(x, np.eye(3)) | |||
|
|||
@pytest.mark.xfail(reason="why would one do it") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nb. These and other corner-cases naturally appear when writing generic code, where you have slices of tensors and then you perform operations on them.
At any rate, these should be easy to fix by simply using the NumPy implementation of this function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turns out ndarray.resize
is documented to differ from np.resize
, so many of these corner cases are in fact not corner cases but documented differences:
If the new array is larger than the original array, then the new array is filled with repeated copies of a. Note that this behavior is different from a.resize(new_shape) which fills with zeros instead of repeated copies of a.
https://numpy.org/doc/stable/reference/generated/numpy.resize.html
One other annoyance is the support of both resize(tuple)
and resize(*tuple)
. Anyhow, this is all done in gh-106.
work through the lists in gh-87.