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
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
15 changes: 15 additions & 0 deletions python/rmm/rmm/_lib/cuda_stream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright (c) 2018-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from rmm.pylibrmm.cuda_stream import CudaStream # noqa: F401
21 changes: 21 additions & 0 deletions python/rmm/rmm/_lib/device_buffer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (c) 2018-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from rmm.pylibrmm.device_buffer import ( # noqa: F401
DeviceBuffer,
copy_device_to_ptr,
copy_host_to_ptr,
copy_ptr_to_host,
to_device,
)
24 changes: 24 additions & 0 deletions python/rmm/rmm/_lib/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright (c) 2018-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from rmm.librmm._logger import logging_level # noqa: F401
from rmm.pylibrmm.logger import ( # noqa: F401
_validate_level_type,
flush_logger,
get_flush_level,
get_logging_level,
set_flush_level,
set_logging_level,
should_log,
)
44 changes: 44 additions & 0 deletions python/rmm/rmm/_lib/memory_resource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright (c) 2018-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# 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.

CallbackMemoryResource,
CudaAsyncMemoryResource,
CudaMemoryResource,
DeviceMemoryResource,
FailureCallbackResourceAdaptor,
FixedSizeMemoryResource,
LimitingResourceAdaptor,
LoggingResourceAdaptor,
ManagedMemoryResource,
PoolMemoryResource,
PrefetchResourceAdaptor,
SamHeadroomMemoryResource,
StatisticsResourceAdaptor,
SystemMemoryResource,
TrackingResourceAdaptor,
UpstreamResourceAdaptor,
_flush_logs,
available_device_memory,
disable_logging,
enable_logging,
get_current_device_resource,
get_current_device_resource_type,
get_log_filenames,
get_per_device_resource_type,
is_initialized,
set_current_device_resource,
set_per_device_resource,
)
Loading