[BUG] 负模板整数会中断内核编译。
作者: alford-auld创建于 2026年9月28日更新于 2026年9月29日
☑️ 我了解严禁使用 AI 编写问题。 描述错误 将负整型模板值传递给内核参数会触发"无法构建"错误,因为它生成了一个无效的 C++ 函数名。 如何重现 代码片段
import mlx.core as mx
kernel = mx.fast.metal_kernel(
name="negtmpl",
input_names=["inp"],
output_names=["out"],
source="out[thread_position_in_grid.x] = T(V);",
)
(out,) = kernel(
inputs=[mx.zeros((4,), dtype=mx.int32)],
template=[("T", mx.int32), ("V", -1)],
grid=(4, 1, 1), threadgroup=(4, 1, 1),
output_shapes=[(4,)], output_dtypes=[mx.int32],
)
mx.eval(out)
print("compiled, out =", out.tolist())预期行为 内核编译成功并返回所有 "-1" 桌面(请填写以下信息) - 操作系统版本: macOS 26.3 (build 25D125) - 版本 0.32.2 - Apple M4 (applegpu_g16g), arm64, Python 3.12.13。 其他上下文 mlx/backend/common/metal_kernel.cpp 中的 make_template_hash 仅重写 <,> 和 ,。负值在名称中生成 "-1"。重现程序由 Claude 代码编写,并由我验证。 追踪信息
Traceback (most recent call last):
File "/private/tmp/mlx-repros/a_negative_template_int.py", line 16, in <module>
mx.eval(out)
RuntimeError: [metal::Device] Unable to build metal library from source
mlx/backend/metal/kernels/utils.h:470:3: error: 'kernel' attribute only applies to functions
[[kernel]] void custom_kernel_negtmpl__int32_t_-1_int32_tc_int32_t(
^
mlx/backend/metal/kernels/utils.h:470:17: error: program scope variable must reside in constant address space
[[kernel]] void custom_kernel_negtmpl__int32_t_-1_int32_tc_int32_t(
^
mlx/backend/metal/kernels/utils.h:470:48: error: expected ';' at end of declaration
[[kernel]] void custom_kernel_negtmpl__int32_t_-1_int32_tc_int32_t(
^
;
mlx/backend/metal/kernels/utils.h:470:48: error: expected external declaration
mlx/backend/metal/kernels/utils.h:470:49: error: expected unqualified-id
[[kernel]] void custom_kernel_negtmpl__int32_t_-1_int32_tc_int32_t(内容来源: ml-explore/mlx