cupy - 检查 `name_expressions` 是否可迭代
问题描述
在尝试jitify
为 CuPy v9.x 计划的新支持时,我发现name_expressions
命名参数 tocupy.RawModule
需要是可迭代的,以便 NVRTC 在以后调用get_function
. 使用 name_expressions 和 nvcc 和/或 path源于 cupy.RawModule 的问题。
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']
or('nameofkernel',)
并且它可以工作。
根据文档https://docs.cupy.dev/en/stable/reference/generated/cupy.RawModule.html,name_expressions
应该作为字符串序列给出。我的建议是检查它name_expressions
是可迭代的(不仅仅是单个 str,即使 str 是可迭代的),以便在调用get_function
.
解决方案
好吧,首先,我们确实说这是一个字符串序列(例如:列表/元组),并在您引用的文档页面中给出了一个示例:
name_expressions
( str序列) – 引用 C++ 全局/模板内核名称的字符串序列(例如列表)。例如,name_expressions=['func1<int>', 'func1<double>', 'func2']
对于模板内核func1<T>
和非模板内核func2
。然后必须一次一个地传递此元组中的字符串get_function()
以检索相应的内核。
所以我看不出有任何歧义。毫无疑问,在 Python 中编写('abc')
并认为它是一个包含 string 的 1 元素元组是一个常见的陷阱'abc'
,为此它应该('abc',)
用逗号来写。但是在代码库中到处检查这样的陷阱将是恕我直言的痛苦。
其次,即使我们添加检查以确保输入是可迭代的,它仍然不能解决您的问题,因为字符串也是可迭代/序列的:
>>> 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)
,这又回到了我上面提到的痛苦。
推荐阅读
- draw.io - Draw.io - 中间有对象的自定义箭头
- android - Android - 开关不显示以编程方式更新的文本开/关值
- python - selenium:@id=contains(., 不适用于单选按钮的 xpath
- .net - 将 Genegic 接口添加到服务中。这段代码是如何工作的?
- spring - 如何设计前端来处理多个后端版本
- arrays - Kotlin 不可变数组
- python - 查找两个数组重叠的算法
- .net - 如何等待 observable 完成?
- c# - 活动设计(Unity3d)
- javascript - 如何下载在 pdf.js 中打开的画布呈现的 pdf 文件?