-
Notifications
You must be signed in to change notification settings - Fork 243
Cythonize _program.py #1565
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
base: main
Are you sure you want to change the base?
Cythonize _program.py #1565
Conversation
- Rename _program.py to _program.pyx - Convert Program to cdef class with _program.pxd declarations - Extract _MembersNeededForFinalize to module-level _ProgramMNFF (nested classes not allowed in cdef class) - Add __repr__ method to Program - Keep ProgramOptions as @DataClass (unchanged) - Keep weakref.finalize pattern for handle cleanup
- Move _translate_program_options to Program_translate_options (cdef) - Move _can_load_generated_ptx to Program_can_load_generated_ptx (cdef) - Remove unused TYPE_CHECKING import block - Follow _memory/_buffer.pyx helper function patterns
- Reorganize file structure per developer guide (principal class first) - Add module docstring, __all__, type alias section - Factor long methods into cdef inline helpers - Add proper exception specs to cdef functions - Fix docstrings (use :class: refs, public paths) - Add type annotations to public methods - Inline _nvvm_exception_manager (single use) - Remove Union import, use | syntax - Add public Program.driver_can_load_nvrtc_ptx_output() API - Update tests to use new public API Closes NVIDIA#1082
|
/ok to test 85dbbb5 |
|
| mod_obj = ObjectCode.from_ptx(ptx, symbol_mapping=sym_map) | ||
| assert mod.code == ptx | ||
| if not Program._can_load_generated_ptx(): | ||
| if not Program.driver_can_load_nvrtc_ptx_output(): |
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 is worthy of discussion. I changed this private API into a public one after refactoring because it seems somewhat useful to an end user. An alternative would be to continue using the private API if the value here is too low.
| class _ProgramMNFF: | ||
| """Members needed for postrm release of program handles.""" | ||
|
|
||
| __slots__ = "handle", "backend" | ||
|
|
||
| def __init__(self, program_obj, handle, backend): | ||
| self.handle = handle | ||
| self.backend = backend | ||
| weakref.finalize(program_obj, self.close) | ||
|
|
||
| def close(self): | ||
| if self.handle is not None: | ||
| if self.backend == "NVRTC": | ||
| handle_return(nvrtc.nvrtcDestroyProgram(self.handle)) | ||
| elif self.backend == "NVVM": | ||
| nvvm = _get_nvvm_module() | ||
| nvvm.destroy_program(self.handle) | ||
| self.handle = None |
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.
I am keeping MNFF and the weakref.finalize cleanup for this PR. In a subsequent PR, I plan to extend the resource handles to nvrtc and nvvm and add nogil blocks.
Summary
Converts
_program.pyto Cython (_program.pyx) for improved code organization and future performance optimization.Programtocdef classwith.pxddeclarationscdef inlinehelpersProgram.driver_can_load_nvrtc_ptx_output()Changes
_program.py→_program.pyxwithcdef class Program_program.pxdwith typed attribute declarations__all__, type alias section added:class:refs and public paths_nvvm_exception_managerinlined (single use)Unionimport removed in favor of|syntaxTest plan
test_program.pytests passtest_module.pytests passpre-commitpassesCloses #1082