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

Numba patch: Enable LTOIR to be linked from memory instead of files #72

Merged
merged 6 commits into from
Apr 19, 2024

Conversation

nvlcambier
Copy link
Contributor

@nvlcambier nvlcambier commented Apr 17, 2024

This PR adds support for linking LTO-IR code from memory in the Numba monkey patch.

This is done by creating a new class LTOIR with kind = 'ltoir'.
Passing an instance of LTOIR to numba.cuda.jit(link=...) will enable linking directly from memory, bypassing the need to write the file to disk with an .ltoir extension.

cc @gmarkall @leofang

@nvlcambier nvlcambier requested a review from a team as a code owner April 17, 2024 19:49
Copy link

copy-pr-bot bot commented Apr 17, 2024

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@leofang
Copy link
Member

leofang commented Apr 17, 2024

/ok to test

@nvlcambier nvlcambier force-pushed the nvlcambier/LTOIR_LinkableCode branch from 2b56226 to 440f99a Compare April 17, 2024 23:34
@jakirkham
Copy link
Member

/ok to test

@leofang
Copy link
Member

leofang commented Apr 17, 2024

@nvlcambier there's a style failure (single -> double quotes)

@nvlcambier
Copy link
Contributor Author

@nvlcambier there's a style failure (single -> double quotes)

Yes I'm waiting for the CI to finish and I'll fix all problems.

@nvlcambier nvlcambier force-pushed the nvlcambier/LTOIR_LinkableCode branch from 440f99a to 90c2d77 Compare April 18, 2024 00:09
@nvlcambier nvlcambier force-pushed the nvlcambier/LTOIR_LinkableCode branch from 90c2d77 to 4ef47fb Compare April 18, 2024 00:09
@jakirkham
Copy link
Member

/ok to test

@jakirkham
Copy link
Member

Looks like the copyright range at the top of the file needs a bump

@isVoid
Copy link
Contributor

isVoid commented Apr 18, 2024

It would be nice to add a unittest in pynvjitlink/tests/test_jit_with_linkable_code.

@jakirkham
Copy link
Member

/ok to test

@isVoid
Copy link
Contributor

isVoid commented Apr 18, 2024

Commit cb4e652 already passes CI. I added unit-test in 08bb96f and see if the unit-test works.

@jakirkham
Copy link
Member

/ok to test

@isVoid
Copy link
Contributor

isVoid commented Apr 18, 2024

Looks like there's an error:

            self._info_log = _nvjitlinklib.get_info_log(self.handle)
            self._error_log = _nvjitlinklib.get_error_log(self.handle)
>           raise NvJitLinkError(f"{e}\n{self.error_log}")
E           pynvjitlink.api.NvJitLinkError: NVJITLINK_ERROR_INVALID_INPUT error when calling nvJitLinkAddData
E           ERROR 1: bad input: does not match type NVJITLINK_INPUT_LTOIR<unnamed-ltoir>
E           

It seems like the ltoir generated by test_binary_generation/makefile isn't the right format for _nvjitlink. Feel free to revert this change if this is not your usage @nvlcambier , and could you add your usage into the unittest?

@nvlcambier
Copy link
Contributor Author

nvlcambier commented Apr 18, 2024

Looks like there's an error:

            self._info_log = _nvjitlinklib.get_info_log(self.handle)
            self._error_log = _nvjitlinklib.get_error_log(self.handle)
>           raise NvJitLinkError(f"{e}\n{self.error_log}")
E           pynvjitlink.api.NvJitLinkError: NVJITLINK_ERROR_INVALID_INPUT error when calling nvJitLinkAddData
E           ERROR 1: bad input: does not match type NVJITLINK_INPUT_LTOIR<unnamed-ltoir>
E           

It seems like the ltoir generated by test_binary_generation/makefile isn't the right format for _nvjitlink. Feel free to revert this change if this is not your usage @nvlcambier , and could you add your usage into the unittest?

Test seems fine. The LTO-IR is built correctly AFAICT. Will try to figure out what's going wrong.

EDIT: the LTO-IR isn't build correctly. nvcc -O3 -rdc true -gencode arch=lto_80,code=lto_80 -dc -o ../pynvjitlink/tests/test_device_functions.ltoir test_device_functions.cu produces an object, not an LTO-IR file (the extension of the file has no bearing). The error message is actually accurate.

nvcc cannot produce .ltoir out of the box. The file being used for
testign is actually an object file. We can use `--keep` to produce
.ltoir. However in that case, in addition, we would need to set
`lto=True` in the linker creation.

This reverts commit 08bb96f.
@nvlcambier nvlcambier force-pushed the nvlcambier/LTOIR_LinkableCode branch from da13adb to 161923a Compare April 18, 2024 20:15
@jakirkham
Copy link
Member

/ok to test

Copy link
Contributor

@gmarkall gmarkall left a comment

Choose a reason for hiding this comment

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

Many thanks, this looks good. Linking LTOIR from memory does work once I added code to patch the linker for LTO, which I will add in a subsequent PR.

@gmarkall gmarkall merged commit edb96ef into rapidsai:main Apr 19, 2024
39 checks passed
gmarkall added a commit that referenced this pull request Apr 25, 2024
#72 added support for linking LTO-IR files from memory instead of disk.
However, it could not be tested because:

- We had no way to generate an LTO-IR container for the test input -
NVCC doesn't support emitting an LTO-IR container, only an LTO-IR
wrapped in a fatbin wrapped in a host object.
- The Numba patch needs to patch Numba for LTO for linking LTO-IR to
work.

This PR implements both of the above, with these changes:

- Addition of a new test binary generator, `generate_raw_ltoir.py`. This
uses NVRTC to generate an LTO-IR container. It uses the cuda-python
bindings, so `cuda-python` is added to the test environments.
- The fixtures had ended up with some duplication and inconsistency
between `conftest.py` and other test files, so these are deduplicated,
and now kept in `conftest.py`. Appropriate updates to all tests using
the fixtures are made.
- The `lto` kwarg is added to the `patch_numba_linker()` function. It is
disabled by default, as enabling LTO will not presently work in all use
cases.
- Added a test for linking LTO-IR from memory.
@gmarkall gmarkall mentioned this pull request Apr 25, 2024
gmarkall added a commit that referenced this pull request Apr 25, 2024
For a new release following the addition of support for linking LTO-IR
from memory (#72), and patching the Numba linker for LTO (#73).
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.

5 participants