Skip to content

Update CtypesLinker::add_cu error message to include fp16 usage - #8744

Merged
sklam merged 14 commits into
numba:mainfrom
testhound:testhound/cu_linking_err_msg
Mar 28, 2023
Merged

sklam merged 14 commits into
numba:mainfrom
testhound:testhound/cu_linking_err_msg

Conversation

@testhound

Copy link
Copy Markdown
Contributor

This pull request update the error message on the add_cu method in CtypesLinker to include fp16. The reason for the update is CUDA fp16 support requires linking of cu files in it's internal implementation.

@testhound
testhound requested a review from gmarkall as a code owner February 8, 2023 07:10
@testhound

Copy link
Copy Markdown
Contributor Author

gpuci run tests

@testhound

Copy link
Copy Markdown
Contributor Author

gpuci run tests

1 similar comment
@gmarkall

Copy link
Copy Markdown
Member

gpuci run tests

@gmarkall gmarkall 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.

Thanks for the PR. In addition to the changed test, I think it would be wise to have a test that causes the exception to be raised through the use of an unsupported operation on float16 data.

A couple of other questions:

  • Is it possible to identify what the user is doing (either using float16 or linking in additional .cu files) and provide them an error message that references specifically what they're doing instead of providing a catch-all generic message?
  • Is the documentation of this restriction sufficient, or are some additions needed in the docs?

Comment thread numba/cuda/cudadrv/driver.py Outdated
Comment on lines +2724 to +2728
raise NotImplementedError("Support for fp16 or linking CUDA source "
"files requires the use of the Nvidia CUDA "
"bindings and enabling the "
"NUMBA_CUDA_USE_NVIDIA_BINDING environment "
"variable. See documentation for me details.")

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.

Suggested changes:

  • fp16 should be float16 -I noticed that the code in Numba is unusual in Python referring to 16-bit floating point values as fp16 rather than float16 which seems to be used everywhere else - for example, the NumPy dtype. This is an inconsistency I should probably have realised much sooner, so I'd like to try not introducing more instances of it.
  • Nvidia is usually written NVIDIA
  • Instead of saying the environment variable should be enabled, say that it should be set to 1.
  • See documentation for me details needs the grammar correcting, and it would be helpful to provide a link to the relevant documentation. Where is the user supposed to look?

@testhound

Copy link
Copy Markdown
Contributor Author

gpuci run tests

@testhound

Copy link
Copy Markdown
Contributor Author

@gmarkall error message updated.

@gmarkall

Copy link
Copy Markdown
Member

Thanks. What do you think about the other two questions from the review?

@gmarkall gmarkall added the 4 - Waiting on author Waiting for author to respond to review label Feb 15, 2023
@testhound

Copy link
Copy Markdown
Contributor Author

Thanks. What do you think about the other two questions from the review?

@gmarkall I don't think it's possible to determine if this is a float 16 issue or the user is linking in .cu files; in both cases we end up in the CtypesLinker::add_cu which throws the exception. We would need to catch this higher up in the stack somewhere.

I think the clarified error message along with the link to the documentation is sufficient.

@gmarkall

Copy link
Copy Markdown
Member

I don't think it's possible to determine if this is a float 16 issue or the user is linking in .cu files; in both cases we end up in the CtypesLinker::add_cu which throws the exception. We would need to catch this higher up in the stack somewhere.

Is there anything wrong with this approach:

diff --git a/numba/cuda/dispatcher.py b/numba/cuda/dispatcher.py
index a79120d85..bb85093f9 100644
--- a/numba/cuda/dispatcher.py
+++ b/numba/cuda/dispatcher.py
@@ -110,6 +110,9 @@ class _Kernel(serialize.ReduceMixin):
                if (f'__numba_wrapper_{fn}' in lib.get_asm_str())]
 
         if res:
+            if not config.CUDA_USE_NVIDIA_BINDING:
+                raise NotImplementedError('float16 needs NV Binding')
+
             # Path to the source containing the foreign function
 
             basedir = os.path.dirname(os.path.abspath(__file__))

which causes the example

from numba import cuda, types

@cuda.jit('void()')
def f():
    types.float16(1) / types.float16(2)

to emit:

  File "/home/gmarkall/numbadev/numba/numba/cuda/dispatcher.py", line 114, in __init__
    raise NotImplementedError('float16 needs NV Binding')
NotImplementedError: float16 needs NV Binding

?

@testhound

Copy link
Copy Markdown
Contributor Author

@gmarkall I was thinking about that after I mentioned "higher up in the stack", and yes that works as well. I'll make that change.

@testhound

Copy link
Copy Markdown
Contributor Author

gpuci run tests

@testhound

Copy link
Copy Markdown
Contributor Author

@gmarkall error message updated.

Comment thread numba/cuda/dispatcher.py Outdated
Comment thread numba/cuda/tests/cudadrv/test_linker.py Outdated
def test_linking_cu_ctypes_unsupported(self):
msg = ('Linking CUDA source files is not supported with the ctypes '
'binding')
msg = ('Support for fp16 or linking CUDA source files '

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.

The expected message doesn't seem to match the message added in dispatcher.py. Shouldn't this test be failing?

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.

@gmarkall the test was not failing it appears because it was not being run due to the skip_with_cuda_python decorator.

@gmarkall

Copy link
Copy Markdown
Member

@testhound Some questions on the diff.

@gmarkall gmarkall added the CUDA CUDA related issue/PR label Feb 16, 2023
@testhound

Copy link
Copy Markdown
Contributor Author

gpuci run tests

1 similar comment
@testhound

Copy link
Copy Markdown
Contributor Author

gpuci run tests

@testhound

Copy link
Copy Markdown
Contributor Author

@gmarkall test case updates made to address your comments.

@gmarkall gmarkall 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.

Thanks for the changes. Whilst running test_linking_cu_ctypes_unsupported in a subprocess is a positive change (so it's good to keep it in), my point was that it doesn't test the changes you're making in this PR, which raises when the automatic linking of a .cufile is induced by the use of a float16 function that requires it - so, another test needs to be added for the new exception that's added here.

There are some other suggestions on the diff relating to spelling errors and simplification.

Comment thread numba/cuda/dispatcher.py
Comment on lines +115 to +119
msg = ("Use of float16 requires the use of the NVIDIA CUDA "
"bindings and settng the "
"NUMBA_CUDA_USE_NVIDIA_BINDING environment variable to "
"1. Relevant documentation is available here:\n"
f"{s}")

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.

Some simplification of this comment (and the typo fix suggested yesterday incorporated):

Suggested change
msg = ("Use of float16 requires the use of the NVIDIA CUDA "
"bindings and settng the "
"NUMBA_CUDA_USE_NVIDIA_BINDING environment variable to "
"1. Relevant documentation is available here:\n"
f"{s}")
msg = ("Use of float16 requires the NVIDIA CUDA bindings and "
"setting the NUMBA_CUDA_USE_NVIDIA_BINDING environment "
f"variable to 1. See {s}")

Comment thread numba/cuda/tests/cudadrv/test_linker.py Outdated
self.assertIn('in the compilation of "error.cu"', msg)

@skip_with_cuda_python
# We need to run the test ina subprocess because the Linker class

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.

Suggested change
# We need to run the test ina subprocess because the Linker class
# We need to run the test in a subprocess because the Linker class

@gmarkall

Copy link
Copy Markdown
Member

@stuartarchibald Could this have a CUDA BF run please?

@gmarkall gmarkall added this to the Numba 0.57 RC milestone Mar 17, 2023
@gmarkall gmarkall added Pending BuildFarm For PRs that have been reviewed but pending a push through our buildfarm and removed 4 - Waiting on author Waiting for author to respond to review labels Mar 17, 2023
gmarkall
gmarkall previously approved these changes Mar 27, 2023

@gmarkall gmarkall 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.

This looks good - just needs a BF run to move to RTM.

@sklam

sklam commented Mar 27, 2023

Copy link
Copy Markdown
Member

BFID numba_smoketest_cuda_yaml_192

@sklam sklam removed the 4 - Waiting on CI Review etc done, waiting for CI to finish label Mar 27, 2023
@sklam

sklam commented Mar 27, 2023

Copy link
Copy Markdown
Member

Buildfarm failed with missing jitlink.cu

======================================================================
FAIL: test_linking_cu_ctypes_unsupported (numba.cuda.tests.cudadrv.test_linker.TestLinker)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/opt/conda/envs/testenv_3e4abae3/lib/python3.9/site-packages/numba/tests/support.py", line 644, in inner
self.subprocess_test_runner(test_module=self.__module__,
File "/opt/conda/envs/testenv_3e4abae3/lib/python3.9/site-packages/numba/tests/support.py", line 629, in subprocess_test_runner
self.assertEqual(status.returncode, 0, streams)
AssertionError: 1 != 0 :
captured stdout:
captured stderr: E
======================================================================
ERROR: test_linking_cu_ctypes_unsupported (numba.cuda.tests.cudadrv.test_linker.TestLinker)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/opt/conda/envs/testenv_3e4abae3/lib/python3.9/site-packages/numba/tests/support.py", line 652, in inner
func(self)
File "/opt/conda/envs/testenv_3e4abae3/lib/python3.9/site-packages/numba/cuda/tests/cudadrv/test_linker.py", line 223, in test_linking_cu_ctypes_unsupported
def f():
File "/opt/conda/envs/testenv_3e4abae3/lib/python3.9/site-packages/numba/cuda/decorators.py", line 133, in _jit
disp.compile(argtypes)
File "/opt/conda/envs/testenv_3e4abae3/lib/python3.9/site-packages/numba/cuda/dispatcher.py", line 929, in compile
kernel.bind()
File "/opt/conda/envs/testenv_3e4abae3/lib/python3.9/site-packages/numba/cuda/dispatcher.py", line 208, in bind
self._codelibrary.get_cufunc()
File "/opt/conda/envs/testenv_3e4abae3/lib/python3.9/site-packages/numba/cuda/codegen.py", line 179, in get_cufunc
cubin = self.get_cubin(cc=device.compute_capability)
File "/opt/conda/envs/testenv_3e4abae3/lib/python3.9/site-packages/numba/cuda/codegen.py", line 158, in get_cubin
linker.add_file_guess_ext(path)
File "/opt/conda/envs/testenv_3e4abae3/lib/python3.9/site-packages/numba/cuda/cudadrv/driver.py", line 2652, in add_file_guess_ext
self.add_cu_file(path)
File "/opt/conda/envs/testenv_3e4abae3/lib/python3.9/site-packages/numba/cuda/cudadrv/driver.py", line 2642, in add_cu_file
with open(path, 'rb') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'numba/cuda/tests/data/jitlink.cu'

@testhound

Copy link
Copy Markdown
Contributor Author

Buildfarm failed with missing jitlink.cu

======================================================================
FAIL: test_linking_cu_ctypes_unsupported (numba.cuda.tests.cudadrv.test_linker.TestLinker)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/opt/conda/envs/testenv_3e4abae3/lib/python3.9/site-packages/numba/tests/support.py", line 644, in inner
self.subprocess_test_runner(test_module=self.__module__,
File "/opt/conda/envs/testenv_3e4abae3/lib/python3.9/site-packages/numba/tests/support.py", line 629, in subprocess_test_runner
self.assertEqual(status.returncode, 0, streams)
AssertionError: 1 != 0 :
captured stdout:
captured stderr: E
======================================================================
ERROR: test_linking_cu_ctypes_unsupported (numba.cuda.tests.cudadrv.test_linker.TestLinker)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/opt/conda/envs/testenv_3e4abae3/lib/python3.9/site-packages/numba/tests/support.py", line 652, in inner
func(self)
File "/opt/conda/envs/testenv_3e4abae3/lib/python3.9/site-packages/numba/cuda/tests/cudadrv/test_linker.py", line 223, in test_linking_cu_ctypes_unsupported
def f():
File "/opt/conda/envs/testenv_3e4abae3/lib/python3.9/site-packages/numba/cuda/decorators.py", line 133, in _jit
disp.compile(argtypes)
File "/opt/conda/envs/testenv_3e4abae3/lib/python3.9/site-packages/numba/cuda/dispatcher.py", line 929, in compile
kernel.bind()
File "/opt/conda/envs/testenv_3e4abae3/lib/python3.9/site-packages/numba/cuda/dispatcher.py", line 208, in bind
self._codelibrary.get_cufunc()
File "/opt/conda/envs/testenv_3e4abae3/lib/python3.9/site-packages/numba/cuda/codegen.py", line 179, in get_cufunc
cubin = self.get_cubin(cc=device.compute_capability)
File "/opt/conda/envs/testenv_3e4abae3/lib/python3.9/site-packages/numba/cuda/codegen.py", line 158, in get_cubin
linker.add_file_guess_ext(path)
File "/opt/conda/envs/testenv_3e4abae3/lib/python3.9/site-packages/numba/cuda/cudadrv/driver.py", line 2652, in add_file_guess_ext
self.add_cu_file(path)
File "/opt/conda/envs/testenv_3e4abae3/lib/python3.9/site-packages/numba/cuda/cudadrv/driver.py", line 2642, in add_cu_file
with open(path, 'rb') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'numba/cuda/tests/data/jitlink.cu'

@sklam this error confuses me as I explicitly fixed the test case to refer to the new location of jitlink.cu and this passes all CI tests which refers to this location.

@gmarkall

Copy link
Copy Markdown
Member

gpuci run tests

@gmarkall

Copy link
Copy Markdown
Member

@sklam this error confuses me as I explicitly fixed the test case to refer to the new location of jitlink.cu and this passes all CI tests which refers to this location.

The test contained a hardcoded expectation of the working directory, which doesn't hold in all cases (in the buildfarm in this instance). Test data locations should be determined using test_data_dir. As we're close to the wire for 0.57 I've pushed a fix for this branch.

@gmarkall

Copy link
Copy Markdown
Member

I've tested this locally on both Linux and Windows, both in the Numba tree (python runtests.py ...) and outside of it (python -m numba.runtests ...) so I'm reasonably confident this should be OK on the buildfarm now. Could this have another buildfarm run please?

@gmarkall gmarkall added the 4 - Waiting on CI Review etc done, waiting for CI to finish label Mar 28, 2023
@sklam

sklam commented Mar 28, 2023

Copy link
Copy Markdown
Member

new BFID: numba_smoketest_cuda_yaml_193

@sklam

sklam commented Mar 28, 2023

Copy link
Copy Markdown
Member

new BFID: numba_smoketest_cuda_yaml_193

passed

@sklam sklam added BuildFarm Passed For PRs that have been through the buildfarm and passed 5 - Ready to merge Review and testing done, is ready to merge and removed Pending BuildFarm For PRs that have been reviewed but pending a push through our buildfarm 4 - Waiting on CI Review etc done, waiting for CI to finish 5 - Ready to merge Review and testing done, is ready to merge labels Mar 28, 2023
@testhound

Copy link
Copy Markdown
Contributor Author

@sklam this error confuses me as I explicitly fixed the test case to refer to the new location of jitlink.cu and this passes all CI tests which refers to this location.

The test contained a hardcoded expectation of the working directory, which doesn't hold in all cases (in the buildfarm in this instance). Test data locations should be determined using test_data_dir. As we're close to the wire for 0.57 I've pushed a fix for this branch.

@gmarkall thank you for the fix.

@gmarkall gmarkall 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.

I think this is good to merge.

@gmarkall

Copy link
Copy Markdown
Member

gpuci run tests

@gmarkall

Copy link
Copy Markdown
Member

Not sure what happened to gpuci, just running again to make sure there's nothing odd lurking.

@gmarkall gmarkall added the 5 - Ready to merge Review and testing done, is ready to merge label Mar 28, 2023
@gmarkall

Copy link
Copy Markdown
Member

Marking as RTM as gpuCI had no surprises.

@sklam
sklam merged commit c1276d7 into numba:main Mar 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

5 - Ready to merge Review and testing done, is ready to merge BuildFarm Passed For PRs that have been through the buildfarm and passed CUDA CUDA related issue/PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants