Skip to content

MAINT,TST: np._from_dlpack: add more test + small memory optimization - #21121

Merged
seberg merged 6 commits into
numpy:mainfrom
tirthasheshpatel:fix-gh20339
Mar 1, 2022
Merged

seberg merged 6 commits into
numpy:mainfrom
tirthasheshpatel:fix-gh20339

Conversation

@tirthasheshpatel

@tirthasheshpatel tirthasheshpatel commented Feb 26, 2022 •

Copy link
Copy Markdown
Contributor

fixes gh-20339

Adds tests for:

  • ndim=0 arrays
  • _from_dlpack called twice with the same capsule (i.e. renaming mechanism)
  • Deallocation of the capsule and arrays when _from_dlpack is called twice.

Also, merged the allocation of DLManagedTensor, shape, and strides. To test if the renaming and deallocating mechanism works as expected, I have added a keyword-only private argument _testing that allows capsules to be passed to _from_dlpack. I don't know how often NumPy does this so let me know if it's fine to keep it or should I revert that change.

cc @seberg @mattip

Comment thread numpy/core/tests/test_dlpack.py Outdated
y = np._from_dlpack(capsule, _testing=True)
with pytest.raises(ValueError, match=r"PyCapsule_GetPointer called "
r"with incorrect name"):
np._from_dlpack(capsule, _testing=True)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think I like a "private" kwarg here, and I don't quite understand why it is necessary. It seems to do nothing except an INCREF and that incref seems odd?!

The allocation change looks nice on first sight though!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think I like a "private" kwarg here, and I don't quite understand why it is necessary.

To check that consumed capsules raise and that the array_dlpack_deleter skips deleting a consumed capsule. But it doesn't have to be tested explicitly so I can revert that part. I added it to address "testing calling _from_dlpack twice" semantics.

@seberg seberg Feb 28, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You can keep the assert if you like, its gone anyway +in release mode (but I am not sure it is useful).

When it comes to checking that we clean up correctly, valgrind or pytest-leaks (sys.gettotalrefcount with a debug python) will find that for us. If that is what you mean?
Getting the line/code paths coverage up is always good, fortunatley, while cumbersome, checking that the code paths clean up correctly is mostly reliable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have reverted. I am not yet aware of how NumPy uses valgrind and pytest-leaks in its tests/CI so I will add the tests back later if needed. Now the PR is stripped down to testing with ndims=0 and memory optimization. Let me know if that looks good!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I have reverted. I am not yet aware of how NumPy uses valgrind and pytest-leaks

It doesn't by default, there is a bit of docs on it on the dev guide, but I am not expecting you to do it. It is more of a: don't worry about it too much, because we would probably catch it eventually.

I see you removed the test again? But the test seems right, the capsule will be renamed so an error should be given, how come you can't keep the test?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

But the test seems right, the capsule will be renamed so an error should be given, how come you can't keep the test?

_from_dlpack doesn't accept capsules; even the one named "dltensor". (it needs an object that has __dlpack__ attribute). So, I had to remove the test (I added the _testing kwargs to enable passing capsule so I could write the test.).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Well, you could create a dummy class with a __dlpack__ dunder to do it. But, we can just focus on the other change in this PR, and then create another (or not, not like it is super vital).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

you could create a dummy class with a __dlpack__ dunder to do it

Good idea! Thanks!

But, we can just focus on the other change in this PR, and then create another

OK, sounds good!

@seberg seberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

One nit, otherwise I think it is ready. Thanks. (I see that one line is indeed not covered by the test right now, so the followup would be cool.)

Comment thread numpy/core/src/multiarray/dlpack.c Outdated
@seberg
seberg merged commit 13e8ace into numpy:main Mar 1, 2022
@seberg

seberg commented Mar 1, 2022

Copy link
Copy Markdown
Member

Thanks for these cleanups, Tirth!

@tirthasheshpatel
tirthasheshpatel deleted the fix-gh20339 branch March 1, 2022 20:38
melissawm pushed a commit to melissawm/numpy that referenced this pull request Apr 12, 2022
…numpy#21121)

* MAINT,TST: np._from_dlpack: add more test + small memory optimization

* MAINT: np._from_dlpack: avoid arithmetic on void *

* MAINT: np._from_dlpack: use unsigned char for _testing

* MAINT: remove an unnecessary NULL check

* MAINT: np._from_dlack: revert passing private kwarg

* MAINT: np._from_dlpack: don't add padding at the end

Co-authored-by: Sebastian Berg <[email protected]>

Co-authored-by: Sebastian Berg <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TST,MAINT: DLPack could use some additional tests and cleanups

2 participants