Skip to content
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

Fix rmm ._lib imports #1693

Merged
merged 5 commits into from
Oct 4, 2024
Merged

Conversation

Matt711
Copy link
Contributor

@Matt711 Matt711 commented Oct 4, 2024

Description

This PR fixes a bug in #1676. It makes sure that rmm imports work correctly using both from rmm._lib... and import rmm._lib... syntax.

Checklist

  • I am familiar with the Contributing Guidelines.
  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

@Matt711 Matt711 requested a review from a team as a code owner October 4, 2024 00:04
@github-actions github-actions bot added the Python Related to RMM Python API label Oct 4, 2024
@Matt711 Matt711 changed the title Fix rmm imports Fix rmm ._lib imports Oct 4, 2024
@Matt711 Matt711 requested a review from vyasr October 4, 2024 00:05
@Matt711 Matt711 added the 5 - DO NOT MERGE Hold off on merging; see PR for details label Oct 4, 2024
# limitations under the License.

from rmm.pylibrmm.memory_resource import ( # noqa: F401
BinningMemoryResource,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These classes are cdef'd, why are they importable from python? Should they be cpdef'd?

Copy link
Contributor

Choose a reason for hiding this comment

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

A cdef class is a normal python class that can also hold C properties. (https://cython.readthedocs.io/en/latest/src/tutorial/cdef_classes.html)

cpdef is for (predominantly) functions which should have a "fast-path" calling route from typed code in cython, and also a "slow-path" python-like calling route.

Copy link
Contributor Author

@Matt711 Matt711 Oct 4, 2024

Choose a reason for hiding this comment

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

So its only cpdef functions that can be called from C and Python?

Edit: I think that's true https://cython.readthedocs.io/en/latest/src/userguide/pyrex_differences.html#cpdef-functions

Copy link
Contributor

Choose a reason for hiding this comment

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

Not exactly.

  • cdef functions can only be called from Cython and in typed contexts (i.e. you cannot pass arbitrary Python objects to them, you need to pass objects that have already been cdefed appropriately themselves).
  • def functions can be called from Python or Cython. They are pure Python functions, so all arguments are treated as normal Python objects.
  • cpdef functions can be called from Python or Cython. When called from Python, they behave like def functions. When called from Cython with untyped arguments, they also behave like def functions. When called from Cython with typed arguments, they behave like cdef functions.

There's more information here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the clarification (def and cpdef make sense to me). Can you give an example of a "typed context"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can a typed context be in a *.py file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it the body of cpdef and cdef functions/methods?

Copy link
Contributor

Choose a reason for hiding this comment

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

cdef class A:
    cpdef class f(self):
        pass

A().f()  # Called like a Python def function
cdef A a = A()
a.f() # Called like a cdef function.

So no, a typed context cannot be in a *.py file, or at least not in the way that we use Cython. With newer versions of Cython you can use py files and compile them too, but the point is that you have to use Cython-specific syntax.

@Matt711 Matt711 removed the 5 - DO NOT MERGE Hold off on merging; see PR for details label Oct 4, 2024
@Matt711 Matt711 self-assigned this Oct 4, 2024
@Matt711 Matt711 added bug Something isn't working non-breaking Non-breaking change labels Oct 4, 2024
Copy link
Contributor

@vyasr vyasr left a comment

Choose a reason for hiding this comment

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

LGTM. I notice that helper.py is missing, but since that only contains a cdef function it's correct not to have it here since it doesn't expose anything that would be visible to Python.

@vyasr
Copy link
Contributor

vyasr commented Oct 4, 2024

I'm going to go ahead and merge because this is currently breaking some users. If we have more changes that we want, we can address them in follow-ups, but I'd like to be able to experiment ASAP to make sure that the underlying issues are resolved in all cases.

@vyasr
Copy link
Contributor

vyasr commented Oct 4, 2024

/merge

@rapids-bot rapids-bot bot merged commit 8150032 into rapidsai:branch-24.12 Oct 4, 2024
57 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working non-breaking Non-breaking change Python Related to RMM Python API
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants