diff --git a/Source/Cmlx/mlx/mlx/compile.cpp b/Source/Cmlx/mlx/mlx/compile.cpp index ca5f0699..48708cd6 100644 --- a/Source/Cmlx/mlx/mlx/compile.cpp +++ b/Source/Cmlx/mlx/mlx/compile.cpp @@ -135,8 +135,9 @@ Compiled::Compiled( } } os << "_"; - for (const auto& x : inputs) { - if (constant_ids.find(x.id()) != constant_ids.end()) { + // Iterate the moved-into members; the parameters are moved-from above. + for (const auto& x : inputs_) { + if (constant_ids_.find(x.id()) != constant_ids_.end()) { continue; } os << kindof(x.dtype()) << x.itemsize(); diff --git a/Tests/MLXTests/TransformTests.swift b/Tests/MLXTests/TransformTests.swift index 6c74f6de..5c7bbad6 100644 --- a/Tests/MLXTests/TransformTests.swift +++ b/Tests/MLXTests/TransformTests.swift @@ -163,6 +163,22 @@ class TransformTests: XCTestCase { XCTAssertEqual(traces, 2) } + func testCompiledKernelsDifferByInputDtype() { + // bf16 and f16 inputs upcast to float32 give the same fused graph, so + // only the input dtypes tell the two kernels apart. + let compiled = compile { (inputs: [MLXArray]) -> [MLXArray] in + let x = inputs[0].asType(.float32) + return [exp(x) * x] + } + + let values = MLXArray(Array(stride(from: Float(-2), to: 2, by: 0.25))) + for dtype in [DType.bfloat16, .float16] { + let x = values.asType(dtype) + let expected = exp(values) * values + assertEqual(compiled([x])[0], expected) + } + } + class CompileTestState: CustomStringConvertible, Updatable { var y: MLXArray var o: MLXArray?