@@ -169,7 +169,7 @@ func TestValidateSeqLen(t *testing.T) {
169169}
170170
171171func TestFlashBackendConfigV_MaskTypeFromVariant (t * testing.T ) {
172- v := fmhaVariant {maskType : "NO_MASK" }
172+ v := fmhaVariant {maskType : "NO_MASK" , elementType : "BF16" }
173173 cfg := flashBackendConfigV (2 , 12 , 2048 , 0.125 , map [string ]any {"x" : 1 }, v )
174174 cfg = strings .ReplaceAll (cfg , " " , "" ) // Remove spaces, to normalize.
175175 if ! strings .Contains (cfg , `"mask_type":"NO_MASK"` ) {
@@ -178,4 +178,26 @@ func TestFlashBackendConfigV_MaskTypeFromVariant(t *testing.T) {
178178 if ! strings .Contains (cfg , `"dropout_rate":0` ) {
179179 t .Errorf ("backend_config missing dropout_rate:\n %s" , cfg )
180180 }
181+ // The intermediate element_type must reflect the variant's dtype (drives f16 vs bf16 backward).
182+ if ! strings .Contains (cfg , `"element_type":"BF16"` ) {
183+ t .Errorf ("backend_config intermediate element_type != BF16:\n %s" , cfg )
184+ }
185+ }
186+
187+ // TestFlashBackendConfigV_ElementTypeFromDtype confirms selectFMHAVariant sets elementType so the
188+ // backend_config intermediate tensor matches the q/k/v dtype (a mismatch fails the f16 backward).
189+ func TestFlashBackendConfigV_ElementTypeFromDtype (t * testing.T ) {
190+ for _ , tc := range []struct {
191+ dtype dtypes.DType
192+ want string
193+ }{
194+ {dtypes .BFloat16 , "BF16" },
195+ {dtypes .Float16 , "F16" },
196+ } {
197+ v , err := selectFMHAVariant ("op" , tc .dtype , true , nil )
198+ require .NoError (t , err )
199+ require .Equal (t , tc .want , v .elementType , "elementType for %s" , tc .dtype )
200+ cfg := strings .ReplaceAll (flashBackendConfigV (1 , 1 , 8 , 1.0 , map [string ]any {"x" : 1 }, v ), " " , "" )
201+ require .Contains (t , cfg , `"element_type":"` + tc .want + `"` , "config element_type for %s" , tc .dtype )
202+ }
181203}
0 commit comments