Skip to content

Commit 70b7501

Browse files
committed
compute/xla: cast bias to bf16 before custom-call in fwd and bwd
Forward and backward flash paths appended options.Bias to operands without casting, producing mixed dtypes (bf16 q/k/v vs f16/f32 bias) that cuDNN rejects. Cast via f.bf16 after validateBias (validation still accepts half/float32 input; the kernel always gets bf16). Also tighten the validateBias doc comment from "broadcastable to [B,H,S,Skv]" to exact shape [B,H,S,S] required (no per-axis broadcast), and trim the dtype-gate comment at line 49.
1 parent 6761260 commit 70b7501

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

compute/xla/flash.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type fmhaVariant struct {
4646
}
4747

4848
// selectFMHAVariant maps the q/k/v dtype and (causal, seqlens, bias) to a cuDNN variant.
49-
// Dtype gate: f16/bf16 only; anything else (incl. fp8 -- paused, no local hardware) ->
49+
// Dtype gate: f16/bf16 only; anything else (incl. fp8) ->
5050
// ErrNotImplemented, and the caller falls back to the decomposed path.
5151
// mask_type derives from causal + seqlens: PADDING_CAUSAL (both), PADDING (seqlens only),
5252
// CAUSAL (causal only), NO_MASK (neither).
@@ -201,8 +201,10 @@ func validateSeqLen(name string, v compute.Value, batch int) error {
201201
return nil
202202
}
203203

204-
// validateBias checks that v is a rank-4 half-precision (or float32) tensor broadcastable
205-
// to [B,H,S,Skv]. name is used in error messages. Returns a wrapped error on mismatch.
204+
// validateBias checks that v is a rank-4 half-precision (or float32) tensor with exact
205+
// shape [B,H,S,S] (self-attention; S==Skv). Per-axis broadcast (e.g. [1,H,S,S]) is not
206+
// accepted by the cuDNN ScaleBias kernel. name is used in error messages. Returns a
207+
// wrapped error on mismatch.
206208
func validateBias(name string, v compute.Value, b, h, s, skv int) error {
207209
n, ok := v.(*Node)
208210
if !ok {
@@ -273,7 +275,11 @@ func (f *Function) FusedScaledDotProductAttention(query, key, value, mask comput
273275
if err = validateBias("Bias", options.Bias, b, h, s, s); err != nil {
274276
return nil, nil, err
275277
}
276-
operands = append(operands, options.Bias)
278+
biasBF16, castErr := f.bf16(options.Bias)
279+
if castErr != nil {
280+
return nil, nil, castErr
281+
}
282+
operands = append(operands, biasBF16)
277283
operandLayouts = append(operandLayouts, []int{3, 2, 1, 0}) // [B,H,S,Skv] row-major
278284
}
279285
if variant.hasSeqLens {
@@ -352,7 +358,11 @@ func (f *Function) FusedScaledDotProductAttentionVJP(query, key, value, mask com
352358
if err = validateBias("Bias", options.Bias, b, h, s, s); err != nil {
353359
return nil, nil, nil, err
354360
}
355-
operands = append(operands, options.Bias)
361+
biasBF16, castErr := f.bf16(options.Bias)
362+
if castErr != nil {
363+
return nil, nil, nil, castErr
364+
}
365+
operands = append(operands, biasBF16)
356366
operandLayouts = append(operandLayouts, []int{3, 2, 1, 0})
357367
}
358368
operands = append(operands, out)

0 commit comments

Comments
 (0)