【问题标题】:Check that `name_expressions` is iterable检查 `name_expressions` 是否可迭代
【发布时间】:2020-12-01 09:11:54
【问题描述】:

在尝试为 CuPy v9.x 计划的新 jitify 支持时,我发现 cupy.RawModulename_expressions 命名参数需要是可迭代的,以便 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',) 并且有效。

根据文档https://docs.cupy.dev/en/stable/reference/generated/cupy.RawModule.htmlname_expressions 应该作为字符串序列给出。我的建议是检查name_expressions 是否可迭代(不只是单个str,即使str 是可迭代的),以便在调用get_function 时捕获其他神秘错误。

【问题讨论】:

    标签: cupy


    【解决方案1】:

    嗯,首先,我们确实说过这是一个字符串的序列(例如:列表/元组),并在您引用的文档页面中给出了一个示例:

    name_expressions (sequence of str) – 引用 C++ 全局/模板内核名称的字符串序列(例如列表)。例如,name_expressions=['func1&lt;int&gt;', 'func1&lt;double&gt;', 'func2'] 用于模板内核 func1&lt;T&gt; 和非模板内核 func2。然后必须将该元组中的字符串一次一个地传递给get_function() 以检索相应的内核。

    所以我看不出有任何歧义。毫无疑问,在 Python 中编写 ('abc') 并认为它是一个包含字符串 'abc' 的 1 元素元组是一个常见的陷阱,为此它应该写为 ('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) 明确检查外,没有其他好方法可以强制执行此检查,这又回到了我上面提到的痛苦。

    【讨论】:

    • 这是真的,我同意,很抱歉打扰了。
    • 别担心,@mikaeltw!
    猜你喜欢
    • 2013-09-23
    • 2023-03-10
    • 1970-01-01
    • 2017-04-05
    • 2017-02-23
    • 2019-12-09
    • 2011-01-04
    • 2021-01-13
    • 1970-01-01
    相关资源
    最近更新 更多