在尝试为CuPy v9.x计划的新的CuPy支持时,我发现name_expressions命名的cupy.RawModule参数需要是可迭代的,以便NVRTC在以后调用get_function时不会失败。问题来源于cupy.RawModule using name_expressions and nvcc and/or path。
def mykernel():
grid = (...)
blocks = (...)
args = (...)
with open('my_cuda_cpp_code.cu') as f:
code = f.read()
kers = ('nameofkernel')
mod = cp.RawModule(code=code, jitify=True, name_expressions=kers, ...)
mod.get_function('nameofkernel')(grid, block, args)上面的代码产生以下错误输出:
Traceback (most recent call last):
File "/home/mikaeltw/env/lib/python3.8/site-packages/cupy/cuda/compiler.py", line 586, in compile
nvrtc.compileProgram(self.ptr, options)
File "cupy_backends/cuda/libs/nvrtc.pyx", line 108, in cupy_backends.cuda.libs.nvrtc.compileProgram
File "cupy_backends/cuda/libs/nvrtc.pyx", line 120, in cupy_backends.cuda.libs.nvrtc.compileProgram
File "cupy_backends/cuda/libs/nvrtc.pyx", line 58, in cupy_backends.cuda.libs.nvrtc.check_status
cupy_backends.cuda.libs.nvrtc.NVRTCError: NVRTC_ERROR_COMPILATION (6)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./jitify_test.py", line 62, in <module>
test_mykernel()
File "./jitify_test.py", line 57, in test_mykernel
mykernel(x_out, x_in)
File "./jitify_test.py", line 50, in mykernel
mod.get_function('nameofkernel')(grid, block, args)
File "cupy/core/raw.pyx", line 470, in cupy.core.raw.RawModule.get_function
File "cupy/core/raw.pyx", line 394, in cupy.core.raw.RawModule.module.__get__
File "cupy/core/raw.pyx", line 402, in cupy.core.raw.RawModule._module
File "cupy/_util.pyx", line 53, in cupy._util.memoize.decorator.ret
File "cupy/core/raw.pyx", line 547, in cupy.core.raw._get_raw_module
File "cupy/core/core.pyx", line 1829, in cupy.core.core.compile_with_cache
File "cupy/core/core.pyx", line 1883, in cupy.core.core.compile_with_cache
File "/home/mikaeltw/env/lib/python3.8/site-packages/cupy/cuda/compiler.py", line 393, in compile_with_cache
return _compile_with_cache_cuda(
File "/home/mikaeltw/env/lib/python3.8/site-packages/cupy/cuda/compiler.py", line 472, in _compile_with_cache_cuda
ptx, mapping = compile_using_nvrtc(
File "/home/mikaeltw/env/lib/python3.8/site-packages/cupy/cuda/compiler.py", line 229, in compile_using_nvrtc
return _compile(source, options, cu_path,
File "/home/mikaeltw/env/lib/python3.8/site-packages/cupy/cuda/compiler.py", line 213, in _compile
ptx, mapping = prog.compile(options, log_stream)
File "/home/mikaeltw/env/lib/python3.8/site-packages/cupy/cuda/compiler.py", line 597, in compile
raise CompileException(log, self.src, self.name, options,
cupy.cuda.compiler.CompileException: __nv_name_map(2): error: expected an expression
__nv_name_map(2): error: Error in parsing name expression for lowered name lookup. Input name expression was: " :"
__nv_name_map(3): error: identifier "_" is undefined
--||--将kers设置为可迭代的,例如['nameofkernel']或('nameofkernel',),它可以工作。
根据docs https://docs.cupy.dev/en/stable/reference/generated/cupy.RawModule.html,name_expressions应该作为字符串序列来给出。我的建议是检查name_expressions是可迭代的(而不仅仅是一个str,尽管str是可迭代的),以便在调用get_function时捕获一个原本神秘的错误。
发布于 2020-12-02 16:28:34
首先,我们确实说这是字符串的序列(ex: list/tuple),并在引用的doc页面中给出了一个示例:
name_expressions(str序列)-引用C++全局/模板内核名称的字符串序列(例如列表)。例如,name_expressions=['func1<int>', 'func1<double>', 'func2']用于模板内核func1<T>和非模板内核func2。然后,必须将这个元组中的字符串一次一个传递给get_function(),以检索相应的内核。
所以我看不出任何含糊不清的地方。毫无疑问,在Python中编写('abc')是一个常见的缺陷,并且认为它是一个包含字符串'abc'的1元素元组,它应该用逗号作为('abc',)编写。但是在代码库中到处检查这样的陷阱将是一个痛苦的屁股IMHO。
其次,即使我们添加了一个检查以确保输入是可迭代的,它仍然不能解决您的问题,因为字符串也是可迭代/序列的:
>>> import collections.abc
>>> isinstance((1,2), collections.abc.Iterable)
True
>>> isinstance((1,2), collections.abc.Sequence)
True
>>> isinstance('abc', collections.abc.Iterable)
True
>>> isinstance('abc', collections.abc.Sequence)
True因此,除了通过isinstance(name_expressions, str)显式地检查检查之外,没有什么好的方法来强制执行此检查,而isinstance(name_expressions, str)又回到了我前面提到的痛苦。
https://stackoverflow.com/questions/65087601
复制相似问题