Skip to content
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

MatMul spec update #2765

Merged
Merged
Changes from 3 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
23 changes: 18 additions & 5 deletions docs/ops/matrix/MatMul_1.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,43 @@

Before matrix multiplication, there is an implicit shape alignment for input arguments. It consists of the following steps:

1. If rank of an input less than 2 it is unsqueezed to 2D tensor by adding axes with size 1 to the left of the shape. For example, if input has shape `[S]` it will be reshaped to `[1, S]`. It is applied for each input independently.
1. One-dimensional tensors unsqueezing is applied for each input independently. The axes inserted in this step are not included in the output shape.
* If rank of the **first** input is less than 2, it is unsqueezed to 2D tensor by adding axes with size 1 to the **left** of the shape. For example `[S]` will be reshaped to `[1, S]`.
* If rank of the **second** input is less than 2, it is unsqueezed to 2D tensor by adding axes with size 1 to the **right** of the shape. For example `[S]` will be reshaped to `[S, 1]`.

2. Applied transpositions specified by optional `transpose_a` and `transpose_b` attributes.

3. If ranks of input arguments are different after steps 1 and 2, each is unsqueezed from the left side of the shape by necessary number of axes to make both shapes of the same rank.

3. Usual rules of the broadcasting are applied for batch dimensions.
4. Usual rules of the broadcasting are applied for batch dimensions.


Output shape inference logic (ND here means bigger than 1D):

* 1D x 1D: `[X] x [Y] -> [1, X] x [Y, 1] -> [1, 1] => [ ]` (scalar)

* 1D x ND: `[X] x [B, ..., X, Y] -> [1, X] x [B, ..., X, Y] -> [B, ..., 1, Y] => [B, ..., Y]`

* ND x 1D: `[B, ..., X, Y] x [Y] -> [B, ..., X, Y] x [Y, 1] -> [B, ..., X, 1] => [B, ..., X]`

* ND x ND: `[B, ..., X, Y] x [B, ..., Y, Z] => [B, ..., X, Z]`


Two attributes, transpose_a and transpose_b specifies embedded transposition for two right-most dimension for the first and the second input tensors correspondingly. It implies swapping of ROW_INDEX_DIM and COL_INDEX_DIM in the corresponding input tensor. Batch dimensions are not affected by these attributes.

**Attributes**

* *transpose_a*

* **Description**: transposes dimensions ROW_INDEX_DIM and COL_INDEX_DIM of the 1st input; 0 means no transpose, 1 means transpose
* **Description**: transposes dimensions ROW_INDEX_DIM and COL_INDEX_DIM of the 1st input; **False** means no transpose, **True** means transpose
* **Range of values**: False or True
* **Type**: boolean
* **Default value**: False
* **Required**: *no*

* *transpose_b*

* **Description**: transposes dimensions ROW_INDEX_DIM and COL_INDEX_DIM of the 2nd input; 0 means no transpose, 1 means transpose
* **Description**: transposes dimensions ROW_INDEX_DIM and COL_INDEX_DIM of the 2nd input; **False** means no transpose, **True** means transpose
* **Range of values**: False or True
* **Type**: boolean
* **Default value**: False
Expand Down Expand Up @@ -65,7 +79,6 @@ Two attributes, transpose_a and transpose_b specifies embedded transposition for
</input>
<output>
<port id="2">
<dim>1</dim>
<dim>1000</dim>
</port>
</output>
Expand Down