Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Source/MLXNN/Transformer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ open class MultiHeadAttention: Module {
public static func createAdditiveCausalMask(_ n: Int, dtype: DType = .float32) -> MLXArray {
let indices = MLXArray(0 ..< n)
var mask = expandedDimensions(indices, axis: 1) .< expandedDimensions(indices, axis: 0)
mask = mask.asType(dtype) * -1e9
mask = mask.asType(dtype) * (dtype.finfo?.min ?? -1e9)
return mask
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// python mlx: 0.32.2.dev20260910+1f8e74e3f
// vendored mlx (Cmlx): 0.32.2
// generator revision: 2
// cases: 3
// cases: 6

import Foundation
import MLX
Expand All @@ -20,6 +20,70 @@ import Testing
@Suite("generated: ModuleAttention")
struct GeneratedModuleAttentionTests {

@Test("createAdditiveCausalMask/float32")
func test_createAdditiveCausalMask_float32() throws {
try withIntegrationState(seed: 10416) {
let result = MultiHeadAttention.createAdditiveCausalMask(4, dtype: .float32)
expectSummary(
result,
ArraySummary(
shape: [4, 4],
dtype: .float32,
mean: -Double.infinity,
minimum: -3.4028234663852886e+38,
maximum: -0.0,
absoluteSum: Double.infinity,
positionChecksum: Double.infinity,
sampleIndices: [0, 3, 6, 9, 12, 15],
samples: [
-0.0, -3.4028234663852886e+38, -3.4028234663852886e+38, -0.0, -0.0, -0.0,
]),
tolerance: .float32)
}
}

@Test("createAdditiveCausalMask/bfloat16")
func test_createAdditiveCausalMask_bfloat16() throws {
try withIntegrationState(seed: 338) {
let result = MultiHeadAttention.createAdditiveCausalMask(4, dtype: .bfloat16)
expectSummary(
result,
ArraySummary(
shape: [4, 4],
dtype: .bfloat16,
mean: -Double.infinity,
minimum: -3.3895313892515355e+38,
maximum: -0.0,
absoluteSum: Double.infinity,
positionChecksum: Double.infinity,
sampleIndices: [0, 3, 6, 9, 12, 15],
samples: [
-0.0, -3.3895313892515355e+38, -3.3895313892515355e+38, -0.0, -0.0, -0.0,
]),
tolerance: .float16)
}
}

@Test("createAdditiveCausalMask/float16")
func test_createAdditiveCausalMask_float16() throws {
try withIntegrationState(seed: 44331) {
let result = MultiHeadAttention.createAdditiveCausalMask(4, dtype: .float16)
expectSummary(
result,
ArraySummary(
shape: [4, 4],
dtype: .float16,
mean: -24564.0,
minimum: -65504.0,
maximum: -0.0,
absoluteSum: 393024.0,
positionChecksum: 147384.0,
sampleIndices: [0, 3, 6, 9, 12, 15],
samples: [-0.0, -65504.0, -65504.0, -0.0, -0.0, -0.0]),
tolerance: .float16)
}
}

@Test("MultiHeadAttention")
func test_MultiHeadAttention() throws {
try withIntegrationState(seed: 24603) {
Expand Down
17 changes: 17 additions & 0 deletions tools/integration_tests/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2214,3 +2214,20 @@ def defaults(name: str, inputs: dict, py: str, swift: str, **kwargs) -> None:
)

EXTRA_IMPORTS["Defaults"] = ["MLXNN"]

# ------------------------------------------------------------ attention mask
#
# `create_additive_causal_mask` is a static function rather than a forward pass,
# so it is a plain case emitted into the `ModuleAttention` file next to the
# `MultiHeadAttention` module cases. One case per dtype: the fill value has to be
# representable in `dtype`, otherwise it overflows to -inf and the positions that
# should be 0 become 0 * -inf == nan.

for _dtype in ("float32", "bfloat16", "float16"):
case(
f"createAdditiveCausalMask/{_dtype}",
"ModuleAttention",
{},
f"nn.MultiHeadAttention.create_additive_causal_mask(4, mx.{_dtype})",
f"MultiHeadAttention.createAdditiveCausalMask(4, dtype: .{_dtype})",
)
Loading