-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Make MultiHeadAttention op return attention probabilities #23125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -226,6 +226,30 @@ void MultiHeadAttentionTypeAndShapeInference(ONNX_NAMESPACE::InferenceContext& c | |
| } | ||
| } | ||
| } | ||
|
|
||
| if (ctx.getNumOutputs() > 3) { // has attention_probs output | ||
| // Output 3 has shape (batch_size, num_heads, sequence_length, total_sequence_length) | ||
| if (hasInputShape(ctx, 0) && hasInputShape(ctx, past_key_index)) { | ||
| auto& query_shape = getInputShape(ctx, 0); | ||
| auto& key_shape = getInputShape(ctx, 1); | ||
| auto& key_seqlen_dim = key_shape.dim()[1]; | ||
| auto& past_seqlen_dim = getInputShape(ctx, past_key_index).dim()[2]; | ||
| if (key_seqlen_dim.has_dim_value() && past_seqlen_dim.has_dim_value()) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a condition of |
||
| auto kv_sequence_length = key_seqlen_dim.dim_value(); | ||
| auto past_sequence_length = past_seqlen_dim.dim_value(); | ||
| int64_t total_sequence_length = kv_sequence_length + past_sequence_length; | ||
| auto num_heads = getAttribute(ctx, "num_heads", 0); | ||
|
|
||
| ONNX_NAMESPACE::TensorShapeProto attention_probs_shape; | ||
| *attention_probs_shape.add_dim() = query_shape.dim()[0]; | ||
| attention_probs_shape.add_dim()->set_dim_value(num_heads); | ||
| *attention_probs_shape.add_dim() = query_shape.dim()[1]; | ||
| attention_probs_shape.add_dim()->set_dim_value(total_sequence_length); | ||
| updateOutputShape(ctx, 3, attention_probs_shape); | ||
| propagateElemTypeFromInputToOutput(ctx, 0, 3); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Type and shape inference for group query attention and sparse attention. | ||
|
|
@@ -1034,6 +1058,11 @@ ONNX_MS_OPERATOR_SET_SCHEMA( | |
| "or present state for self attention value with shape (batch_size, num_heads, total_sequence_length, head_size)", | ||
| "T", | ||
| OpSchema::Optional) | ||
| .Output(3, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You will need update documents (You can find the updated documents in artifacts of |
||
| "attention_probs", | ||
| "Attention probabilities with shape (batch_size, num_heads, sequence_length, total_sequence_length)", | ||
| "T", | ||
| OpSchema::Optional) | ||
| .TypeConstraint("T", {"tensor(float)", "tensor(float16)"}, "Constrain input and output to float tensors.") | ||
| .TypeConstraint("M", {"tensor(int32)"}, "Constrain mask to integer types") | ||
| .TypeAndShapeInferenceFunction([](ONNX_NAMESPACE::InferenceContext& ctx) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no need to allocate extra space if we do not output it. You can follow the handling of output_qk (temp result of
q*kbefore softmax) in this function.If we do not output both
q*kandsoftmax(q*k), we can consolidate them together by using a boolean flag to indicate whether we need output the one before softmax or after softmax.