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
3 changes: 3 additions & 0 deletions onnxscript/rewriter/ort_fusions/sdpa.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ def pattern(
)

attn_weight = op.Softmax(attn_score, axis=-1)
is_nan = op.IsNaN(attn_weight)
adj_attn_weight = op.Where(is_nan, 0.0, attn_weight)
attn_weight = pattern.OrValue([adj_attn_weight, attn_weight])
attn_output = op.MatMul(attn_weight, value)
return attn_output

Expand Down
85 changes: 68 additions & 17 deletions onnxscript/rewriter/ort_fusions/sdpa_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ def _unmasked_pre_div_sdpa_script(query, key, value):
scaled_key = op.Div(key_transposed, divisor)
attn_score = op.MatMul(scaled_query, scaled_key)
attn_weight = op.Softmax(attn_score, axis=-1)
attn_output = op.MatMul(attn_weight, value)
is_nan = op.IsNaN(attn_weight)
zero = op.Constant(value_float=0.0)
adj_attn_weight = op.Where(is_nan, zero, attn_weight)
attn_output = op.MatMul(adj_attn_weight, value)
return attn_output


Expand All @@ -56,7 +59,10 @@ def _unmasked_pre_mul_sdpa_script(query, key, value):
scaled_key = op.Mul(key_transposed, multiplier)
attn_score = op.MatMul(scaled_query, scaled_key)
attn_weight = op.Softmax(attn_score, axis=-1)
attn_output = op.MatMul(attn_weight, value)
is_nan = op.IsNaN(attn_weight)
zero = op.Constant(value_float=0.0)
adj_attn_weight = op.Where(is_nan, zero, attn_weight)
attn_output = op.MatMul(adj_attn_weight, value)
return attn_output


Expand All @@ -67,7 +73,10 @@ def _unmasked_post_div_sdpa_script(query, key, value):
attn_score = op.MatMul(query, key_transposed)
scaled_attn_score = op.Div(attn_score, divisor)
attn_weight = op.Softmax(scaled_attn_score, axis=-1)
attn_output = op.MatMul(attn_weight, value)
is_nan = op.IsNaN(attn_weight)
zero = op.Constant(value_float=0.0)
adj_attn_weight = op.Where(is_nan, zero, attn_weight)
attn_output = op.MatMul(adj_attn_weight, value)
return attn_output


Expand All @@ -78,7 +87,10 @@ def _unmasked_post_mul_sdpa_script(query, key, value):
attn_score = op.MatMul(query, key_transposed)
scaled_attn_score = op.Mul(attn_score, multiplier)
attn_weight = op.Softmax(scaled_attn_score, axis=-1)
attn_output = op.MatMul(attn_weight, value)
is_nan = op.IsNaN(attn_weight)
zero = op.Constant(value_float=0.0)
adj_attn_weight = op.Where(is_nan, zero, attn_weight)
attn_output = op.MatMul(adj_attn_weight, value)
return attn_output


Expand All @@ -90,7 +102,10 @@ def _custom_scale_pre_div_sdpa_script(query, key, value):
scaled_key = op.Div(key_transposed, divisor)
attn_score = op.MatMul(scaled_query, scaled_key)
attn_weight = op.Softmax(attn_score, axis=-1)
attn_output = op.MatMul(attn_weight, value)
is_nan = op.IsNaN(attn_weight)
zero = op.Constant(value_float=0.0)
adj_attn_weight = op.Where(is_nan, zero, attn_weight)
attn_output = op.MatMul(adj_attn_weight, value)
return attn_output


Expand All @@ -102,7 +117,10 @@ def _custom_scale_pre_mul_sdpa_script(query, key, value):
scaled_key = op.Mul(key_transposed, multiplier)
attn_score = op.MatMul(scaled_query, scaled_key)
attn_weight = op.Softmax(attn_score, axis=-1)
attn_output = op.MatMul(attn_weight, value)
is_nan = op.IsNaN(attn_weight)
zero = op.Constant(value_float=0.0)
adj_attn_weight = op.Where(is_nan, zero, attn_weight)
attn_output = op.MatMul(adj_attn_weight, value)
return attn_output


Expand All @@ -115,7 +133,10 @@ def _custom_multi_scale_pre_mul_sdpa_script(query, key, value):
scaled_key = op.Mul(key_transposed, multiplier_k)
attn_score = op.MatMul(scaled_query, scaled_key)
attn_weight = op.Softmax(attn_score, axis=-1)
attn_output = op.MatMul(attn_weight, value)
is_nan = op.IsNaN(attn_weight)
zero = op.Constant(value_float=0.0)
adj_attn_weight = op.Where(is_nan, zero, attn_weight)
attn_output = op.MatMul(adj_attn_weight, value)
return attn_output


Expand All @@ -126,7 +147,10 @@ def _custom_scale_post_div_sdpa_script(query, key, value):
attn_score = op.MatMul(query, key_transposed)
scaled_attn_score = op.Div(attn_score, divisor)
attn_weight = op.Softmax(scaled_attn_score, axis=-1)
attn_output = op.MatMul(attn_weight, value)
is_nan = op.IsNaN(attn_weight)
zero = op.Constant(value_float=0.0)
adj_attn_weight = op.Where(is_nan, zero, attn_weight)
attn_output = op.MatMul(adj_attn_weight, value)
return attn_output


Expand All @@ -137,7 +161,10 @@ def _custom_scale_post_mul_sdpa_script(query, key, value):
attn_score = op.MatMul(query, key_transposed)
scaled_attn_score = op.Mul(attn_score, multiplier)
attn_weight = op.Softmax(scaled_attn_score, axis=-1)
attn_output = op.MatMul(attn_weight, value)
is_nan = op.IsNaN(attn_weight)
zero = op.Constant(value_float=0.0)
adj_attn_weight = op.Where(is_nan, zero, attn_weight)
attn_output = op.MatMul(adj_attn_weight, value)
return attn_output


Expand All @@ -150,7 +177,10 @@ def _masked_pre_div_sdpa_script(query, key, value, mask):
attn_score = op.MatMul(scaled_query, scaled_key)
masked_attn_score = op.Add(attn_score, mask)
attn_weight = op.Softmax(masked_attn_score, axis=-1)
attn_output = op.MatMul(attn_weight, value)
is_nan = op.IsNaN(attn_weight)
zero = op.Constant(value_float=0.0)
adj_attn_weight = op.Where(is_nan, zero, attn_weight)
attn_output = op.MatMul(adj_attn_weight, value)
return attn_output


Expand All @@ -163,7 +193,10 @@ def _masked_pre_mul_sdpa_script(query, key, value, mask):
attn_score = op.MatMul(scaled_query, scaled_key)
masked_attn_score = op.Add(attn_score, mask)
attn_weight = op.Softmax(masked_attn_score, axis=-1)
attn_output = op.MatMul(attn_weight, value)
is_nan = op.IsNaN(attn_weight)
zero = op.Constant(value_float=0.0)
adj_attn_weight = op.Where(is_nan, zero, attn_weight)
attn_output = op.MatMul(adj_attn_weight, value)
return attn_output


Expand All @@ -175,7 +208,10 @@ def _masked_post_div_sdpa_script(query, key, value, mask):
scaled_attn_score = op.Div(attn_score, divisor)
masked_attn_score = op.Add(scaled_attn_score, mask)
attn_weight = op.Softmax(masked_attn_score, axis=-1)
attn_output = op.MatMul(attn_weight, value)
is_nan = op.IsNaN(attn_weight)
zero = op.Constant(value_float=0.0)
adj_attn_weight = op.Where(is_nan, zero, attn_weight)
attn_output = op.MatMul(adj_attn_weight, value)
return attn_output


Expand All @@ -187,7 +223,10 @@ def _masked_post_mul_sdpa_script(query, key, value, mask):
scaled_attn_score = op.Mul(attn_score, multiplier)
masked_attn_score = op.Add(scaled_attn_score, mask)
attn_weight = op.Softmax(masked_attn_score, axis=-1)
attn_output = op.MatMul(attn_weight, value)
is_nan = op.IsNaN(attn_weight)
zero = op.Constant(value_float=0.0)
adj_attn_weight = op.Where(is_nan, zero, attn_weight)
attn_output = op.MatMul(adj_attn_weight, value)
return attn_output


Expand All @@ -200,7 +239,10 @@ def _masked_custom_scale_pre_div_sdpa_script(query, key, value, mask):
attn_score = op.MatMul(scaled_query, scaled_key)
masked_attn_score = op.Add(attn_score, mask)
attn_weight = op.Softmax(masked_attn_score, axis=-1)
attn_output = op.MatMul(attn_weight, value)
is_nan = op.IsNaN(attn_weight)
zero = op.Constant(value_float=0.0)
adj_attn_weight = op.Where(is_nan, zero, attn_weight)
attn_output = op.MatMul(adj_attn_weight, value)
return attn_output


Expand All @@ -213,7 +255,10 @@ def _masked_custom_scale_pre_mul_sdpa_script(query, key, value, mask):
attn_score = op.MatMul(scaled_query, scaled_key)
masked_attn_score = op.Add(attn_score, mask)
attn_weight = op.Softmax(masked_attn_score, axis=-1)
attn_output = op.MatMul(attn_weight, value)
is_nan = op.IsNaN(attn_weight)
zero = op.Constant(value_float=0.0)
adj_attn_weight = op.Where(is_nan, zero, attn_weight)
attn_output = op.MatMul(adj_attn_weight, value)
return attn_output


Expand All @@ -225,7 +270,10 @@ def _masked_custom_scale_post_div_sdpa_script(query, key, value, mask):
scaled_attn_score = op.Div(attn_score, divisor)
masked_attn_score = op.Add(scaled_attn_score, mask)
attn_weight = op.Softmax(masked_attn_score, axis=-1)
attn_output = op.MatMul(attn_weight, value)
is_nan = op.IsNaN(attn_weight)
zero = op.Constant(value_float=0.0)
adj_attn_weight = op.Where(is_nan, zero, attn_weight)
attn_output = op.MatMul(adj_attn_weight, value)
return attn_output


Expand All @@ -237,7 +285,10 @@ def _masked_custom_scale_post_mul_sdpa_script(query, key, value, mask):
scaled_attn_score = op.Mul(attn_score, multiplier)
masked_attn_score = op.Add(scaled_attn_score, mask)
attn_weight = op.Softmax(masked_attn_score, axis=-1)
attn_output = op.MatMul(attn_weight, value)
is_nan = op.IsNaN(attn_weight)
zero = op.Constant(value_float=0.0)
adj_attn_weight = op.Where(is_nan, zero, attn_weight)
attn_output = op.MatMul(adj_attn_weight, value)
return attn_output


Expand Down
Loading