8000 implement more numpy functions by ev-br · Pull Request #104 · Quansight-Labs/numpy_pytorch_interop · GitHub
[go: up one dir, main page]

Skip to content

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

Closed
wants to merge 3 commits into from
Closed

implement more numpy functions #104

wants to merge 3 commits into from

Conversation

ev-br
Copy link
Collaborator
@ev-br ev-br commented Apr 3, 2023

work through the lists in gh-87.

  • copyto
  • resize
  • histogram


@pytest.mark.xfail(reason="how to find if someone is refencing an array")
Copy link
Collaborator

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.

Comment on lines +1061 to +1078
@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)
Copy link
Collaborator

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?

Copy link
Collaborator Author

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")
Copy link
Collaborator

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.

Copy link
Collaborator Author

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.

@ev-br
Copy link
Collaborator Author
ev-br commented Apr 5, 2023

Continued in gh-106 to pick up the refactor from gh-105.

86E2

@ev-br ev-br closed this Apr 5, 2023
@ev-br ev-br deleted the resize branch April 17, 2023 08:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0