【问题标题】:Any suggestions when it shows " TypeError: not enough arguments for format string " in Python?当它在 Python 中显示“TypeError: not enough arguments for format string”时有什么建议吗?
【发布时间】:2021-08-04 10:39:24
【问题描述】:

当我尝试通过 pycuda 运行矩阵乘法示例时。

kernel_code_template = """
__global__ void MatrixMulKernel(float *a,float *b,float *c){
    int tx = threadIdx.x;
    int ty = threadIdx.y;
    float Pvalue = 0;
    for(int i=0; i<%(N)s; ++i){
        float Aelement = a[ty * %(N)s + i];
        float Belement = b[i * %(M)s + tx];
        Pvalue += Aelement * Belement;
    }
    c[ty * %[M]s + tx] = Pvalue;
}
"""

M, N = 2, 3
kernel_code = kernel_code_template % {'M': M, 'N': N}

它报告了如下错误:

kernel_code = kernel_code_template % {'M': M, 'N': N}
TypeError: not enough arguments for format string

我试过检查“%”标记是否有任何问题,但什么都没有。

【问题讨论】:

  • 您使用的是哪个版本的 Python?
  • 3.7 @Thomas
  • 干杯,添加了我的答案。
  • " %[M]s" -- 你打错了

标签: python cuda pycuda


【解决方案1】:

我认为您正在混合语法,%.format 字符串替换。在这里查看一个不错的摘要: https://pyformat.info/

现在我发现了错误(第 11 行):%[M]s --> %(M)s

【讨论】:

  • sry ,我到现在还没有看到@Brandt
【解决方案2】:

要直接回答问题,您需要将代码中的%[M]s 更改为%(M)s,如下所示:

kernel_code_template = """
__global__ void MatrixMulKernel(float *a,float *b,float *c){
    int tx = threadIdx.x;
    int ty = threadIdx.y;
    float Pvalue = 0;
    for(int i=0; i<%(N)s; ++i){
        float Aelement = a[ty * %(N)s + i];
        float Belement = b[i * %(M)s + tx];
        Pvalue += Aelement * Belement;
    }
    c[ty * %(M)s + tx] = Pvalue;
}
"""

然后它将按预期工作。但是,我强烈建议您开始使用 f 字符串,因为您表示您使用的是 Python 3.7:

kernel_code_template = f"""
__global__ void MatrixMulKernel(float *a,float *b,float *c){{
    int tx = threadIdx.x;
    int ty = threadIdx.y;
    float Pvalue = 0;
    for(int i=0; i<{N}; ++i){{
        float Aelement = a[ty * {N} + i];
        float Belement = b[i * {M} + tx];
        Pvalue += Aelement * Belement;
    }}
    c[ty * {M} + tx] = Pvalue;
}}
"""

不过,我知道有一些取舍,即 {{}} 转义。正如@Brandt 指出的那样,还有.format() 方法。选择最适合您的。

【讨论】:

  • 非常感谢!它运行良好,你们都很好!还有一件事,在第二种方法(使用 {M} 和 {N} )中,必须在内核之前写入行 'M, N = 2, 3' 否则它会将 M&N 报告为“未定义”。这很有趣,我猜这是因为计算机需要分别在 CPU 和 GPU 中分配空间。 @托马斯
  • 没问题。如果答案对您有所帮助,请随时点赞和/或接受答案。至于对 CPU 和 GPU 的评论,这是因为 M 和 N 在您的代码中没有定义符号,直到它们被显式分配,所以它更多地与进程使用的内存有关;你可以在没有 GPU 的计算机上运行这段代码 sn-p!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-29
  • 1970-01-01
  • 2019-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多