Implement nGraph transformation to decompose Einsum-7 operation#5529
Merged
ilyachur merged 12 commits intoopenvinotoolkit:masterfrom May 18, 2021
Merged
Conversation
Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com>
Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com>
rkazants
commented
May 13, 2021
...e-engine/src/transformations/include/transformations/op_conversions/einsum_decomposition.hpp
Outdated
Show resolved
Hide resolved
Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com>
…einsum_ngraph_transformation
Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com>
Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com>
Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com>
sadolini
reviewed
May 14, 2021
...rence-engine/src/transformations/src/transformations/op_conversions/einsum_decomposition.cpp
Outdated
Show resolved
Hide resolved
...rence-engine/src/transformations/src/transformations/op_conversions/einsum_decomposition.cpp
Outdated
Show resolved
Hide resolved
...rence-engine/src/transformations/src/transformations/op_conversions/einsum_decomposition.cpp
Show resolved
Hide resolved
...rence-engine/src/transformations/src/transformations/op_conversions/einsum_decomposition.cpp
Outdated
Show resolved
Hide resolved
...rence-engine/src/transformations/src/transformations/op_conversions/einsum_decomposition.cpp
Show resolved
Hide resolved
rkazants
commented
May 14, 2021
...rence-engine/src/transformations/src/transformations/op_conversions/einsum_decomposition.cpp
Outdated
Show resolved
Hide resolved
rkazants
commented
May 14, 2021
...engine/src/transformations/src/transformations/common_optimizations/common_optimizations.cpp
Show resolved
Hide resolved
popovaan
approved these changes
May 14, 2021
...rence-engine/src/transformations/src/transformations/op_conversions/einsum_decomposition.cpp
Outdated
Show resolved
Hide resolved
Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com>
sadolini
approved these changes
May 17, 2021
...e-engine/src/transformations/include/transformations/op_conversions/einsum_decomposition.hpp
Outdated
Show resolved
Hide resolved
...rence-engine/src/transformations/src/transformations/op_conversions/einsum_decomposition.cpp
Outdated
Show resolved
Hide resolved
...rence-engine/src/transformations/src/transformations/op_conversions/einsum_decomposition.cpp
Outdated
Show resolved
Hide resolved
...rence-engine/src/transformations/src/transformations/op_conversions/einsum_decomposition.cpp
Outdated
Show resolved
Hide resolved
...rence-engine/src/transformations/src/transformations/op_conversions/einsum_decomposition.cpp
Show resolved
Hide resolved
...rence-engine/src/transformations/src/transformations/op_conversions/einsum_decomposition.cpp
Outdated
Show resolved
Hide resolved
lazarevevgeny
suggested changes
May 17, 2021
...e-engine/src/transformations/include/transformations/op_conversions/einsum_decomposition.hpp
Outdated
Show resolved
Hide resolved
inference-engine/tests/functional/inference_engine/ngraph_reader/einsum_tests.cpp
Show resolved
Hide resolved
...rence-engine/src/transformations/src/transformations/op_conversions/einsum_decomposition.cpp
Outdated
Show resolved
Hide resolved
...rence-engine/src/transformations/src/transformations/op_conversions/einsum_decomposition.cpp
Outdated
Show resolved
Hide resolved
...rence-engine/src/transformations/src/transformations/op_conversions/einsum_decomposition.cpp
Show resolved
Hide resolved
lazarevevgeny
suggested changes
May 17, 2021
...rence-engine/src/transformations/src/transformations/op_conversions/einsum_decomposition.cpp
Outdated
Show resolved
Hide resolved
...rence-engine/src/transformations/src/transformations/op_conversions/einsum_decomposition.cpp
Outdated
Show resolved
Hide resolved
...rence-engine/src/transformations/src/transformations/op_conversions/einsum_decomposition.cpp
Show resolved
Hide resolved
...rence-engine/src/transformations/src/transformations/op_conversions/einsum_decomposition.cpp
Outdated
Show resolved
Hide resolved
Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com>
…einsum_ngraph_transformation Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com>
Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com>
Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com>
GlebKazantaev
approved these changes
May 18, 2021
ilyachur
reviewed
May 18, 2021
...rence-engine/src/transformations/src/transformations/op_conversions/einsum_decomposition.cpp
Show resolved
Hide resolved
...rence-engine/src/transformations/src/transformations/op_conversions/einsum_decomposition.cpp
Show resolved
Hide resolved
...rence-engine/src/transformations/src/transformations/op_conversions/einsum_decomposition.cpp
Show resolved
Hide resolved
...rence-engine/src/transformations/src/transformations/op_conversions/einsum_decomposition.cpp
Show resolved
Hide resolved
lazarevevgeny
approved these changes
May 18, 2021
Contributor
lazarevevgeny
left a comment
There was a problem hiding this comment.
In general it looks good. I didn't go into details of logic of some parts of this PR but I trust Roman.
But I agree with Ilya's comment about moving some methods to private section of the class instead of passing transformation instance pointer.
ilyachur
approved these changes
May 18, 2021
Contributor
ilyachur
left a comment
There was a problem hiding this comment.
Please fix comments in the next PR
yekruglov
pushed a commit
to yekruglov/openvino
that referenced
this pull request
Jun 7, 2021
…vinotoolkit#5529) * Implement nGraph transformation to decompose Einsum-7 operation Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com> * Use MatMul instead of Eltwise-multiplication and ReduceSum Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com> * Add description for new methods Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com> * Fix code style Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com> * Fix code style #2 Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com> * Remove unused variables.py Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com> * Apply feedback after review: fix comments, new_register_node use Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com> * Add Reshape if needed and apply code-review feedback Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com> * Fix code-style Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com> * Remove unused variable Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com>
rnugmanx
pushed a commit
to rnugmanx/openvino
that referenced
this pull request
Aug 26, 2021
…vinotoolkit#5529) * Implement nGraph transformation to decompose Einsum-7 operation Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com> * Use MatMul instead of Eltwise-multiplication and ReduceSum Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com> * Add description for new methods Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com> * Fix code style Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com> * Fix code style #2 Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com> * Remove unused variables.py Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com> * Apply feedback after review: fix comments, new_register_node use Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com> * Add Reshape if needed and apply code-review feedback Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com> * Fix code-style Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com> * Remove unused variable Signed-off-by: Roman Kazantsev <roman.kazantsev@intel.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Ticket: 54320
Description: Currently the plugins do not support Einsum-7 operation. The implemented nGraph transformation decomposes Einsum-7 operation into a sub-graph that contains operations supported by the plugins. The resulted sub-graph can vary too much and depends on Einsum equation. The sub-graph can include MatMul, Reshape, ReduceSum, Unsqueeze, Multiply operations. The quick check of inference performance on the BERT model including 97 Einsum operations proves the functional correctness of the transformation and does not unveil performance degradation. Also, the transformation is tested on 19 layer tests.
Current Limitations:
aab,bc->acis unsupported due to repeated labelain the first input subscript. In the meantime Einsum operation with equation=abc,dcb->acis supported.einsum_pathfor multiple operand case. And in such cases the operands are contracted consequently. The current implementation is sufficient for now because we only have models with two operand Einsum operation.Details: The main idea of the transformation is better to present using an example about how numpy einsum operation can be decomposed into simple numpy operations. Let us consider the following code:
input1 = np.random.random_integers(10, size=(2,3,4,5)).astype(np.float)input2 = np.random.random_integers(10, size=(6,4,5,3)).astype(np.float)ref_result = np.einsum('aecd,bcde->abe', input1, input2)In the first step let us group dimensions of both operands into three groups:
e.candd.ais separate for the first operand; dimension with a labelb- for the second operand.Transpose the first operand so that it has the common dimensions first, the separate dimensions after, and the reduced dimensions lastly. Transpose the second operand so that it has the common dimensions first, the reduced dimensions after, and the separate dimensions lastly. So both subscripts look like as follows:
eacd- for the first operands,ecdb- for the second operandinput1_grouped = np.transpose(input1, [1, 0, 2, 3])input2_grouped = np.transpose(input2, [3, 1, 2, 0])In the second step let us collapse separate dimensions into one dimension using
Reshapeoperation and do the same for reduced dimensions. It is needed to utilizeMatMuloperation that has requirements for input format. The common dimensions are sort of batch dimensions forMatMul operation.input1_reshaped = np.reshape(input1_grouped, (3, 2, 20))input2_reshaped = np.reshape(input2_grouped, (3, 20, 6))In the third step, perform
MatMul operation.matmul = np.matmul(input1_reshaped, input2_reshaped)The result of
MatMuloperation will stay the common and separate dimensions.In the fourth step, unroll previously collapsed dimensions (the separate dimensions) using
Reshapeoperation. In this case it does not have collapsed the separate dimensions.matmul_reshaped = np.reshape(matmul, (3, 2, 6))Finally, it needs to adjust layout specified by the output subcript. The intermediate result has a layout corresponding to
eabsubscript but the output subscipt isabe. Hence, perform the transpose.result = np.transpose(matmul_reshaped, [1, 2, 0])Values of

ref_resultandresultare matched. The nGraph transformation relies on this idea and its modification to avoid extra transpose usingtransposeattributes inMatMuloperations.Consider IR with Einsum operation and how it is decomposed. The original IR looks as follows:
The resulted IR before constant-folding:

The idea described above is generalized to multiple operand case by computing intermediate output subscript for each pair of operands.
Signed-off-by: Roman Kazantsev roman.kazantsev@intel.com