1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-31 00:03:03 +04:00

Merge pull request #24509 from Abdurrahheem:ash/dev_einsum_fast_gemm

Fast gemm for einsum #24509

## This PR adds performance tests for Einsum Layer with FastGemm. See below results of performance test on different inputs

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Abduragim Shtanchaev
2023-11-16 17:20:17 +04:00
committed by GitHub
parent 83d70b0f36
commit 8c10545d3c
2 changed files with 141 additions and 145 deletions
+25 -39
View File
@@ -11,19 +11,16 @@ struct EinsumParams {
int outputSize;
std::string equation;
std::vector<MatShape> einsumInpShapes;
EinsumParams(std::string equation_, int inputSize_, int outputSize_, std::vector<MatShape> einsumInpShapes_ = std::vector<MatShape>())
EinsumParams(std::string equation_, std::vector<MatShape> einsumInpShapes_ = std::vector<MatShape>())
{
inputSize = inputSize_;
outputSize = outputSize_;
inputSize = einsumInpShapes_.size();
equation = equation_;
einsumInpShapes = einsumInpShapes_;
}
};
static inline void PrintTo(const EinsumParams& params, ::std::ostream* os) {
(*os) << "Eqiation=" << params.equation << ", "
<< "InputSize=" << params.inputSize << ", "
<< "OutputSize=" << params.outputSize << ", ";
(*os) << "Equation=" << params.equation << " ";
(*os) << "InputShape={";
for(int i = 0; i < params.einsumInpShapes.size(); i++)
@@ -41,22 +38,22 @@ static inline void PrintTo(const EinsumParams& params, ::std::ostream* os) {
// test cases
static const EinsumParams testEinsumConfigs[] = {
// TODO: Add tests with one input after ellips merge
{"ij, jk -> ik", 2, 1, {{2, 3}, {3, 2}}},
{"ij, jk -> ik", 2, 1, {{20, 30}, {30, 20}}},
{"ij, jk -> ik", 2, 1, {{113, 127}, {127, 113}}},
{"ij, jk -> ik", {{2, 3}, {3, 2}}},
{"ij, jk -> ik", {{20, 30}, {30, 20}}},
{"ij, jk -> ik", {{113, 127}, {127, 113}}},
{"imkj, injs -> imnks", 2, 1, {{1, 4, 7, 9}, {1, 5, 9, 8}}},
{"imkj, injs -> imnks", 2, 1, {{1, 4, 70, 90}, {1, 5, 90, 80}}},
{"imkj, injs -> imnks", 2, 1, {{1, 4, 73, 91}, {1, 5, 91, 57}}},
{"imkj, injs -> imnks", {{1, 4, 7, 9}, {1, 5, 9, 8}}},
{"imkj, injs -> imnks", {{1, 4, 70, 90}, {1, 5, 90, 80}}},
{"imkj, injs -> imnks", {{1, 4, 73, 91}, {1, 5, 91, 57}}},
{"ij -> i", 1, 1, {{30, 40}}},
{"ij -> i", 1, 1, {{113, 374}}},
{"ij -> i", {{30, 40}}},
{"ij -> i", {{113, 374}}},
{"...ij -> ...i", 1, 1, {{30, 40}}},
{"...ij -> ...i", 1, 1, {{113, 374}}},
{"...ij -> ...i", {{30, 40}}},
{"...ij -> ...i", {{113, 374}}},
{"...ij, ...jk -> ...ik", 2, 1, {{40, 50}, {50, 80}}},
{"...ij, ...jk -> ...ik", 2, 1, {{47, 51}, {51, 83}}},
{"...ij, ...jk -> ...ik", {{40, 50}, {50, 80}}},
{"...ij, ...jk -> ...ik", {{47, 51}, {51, 83}}},
};
class Layer_Einsum: public TestBaseWithParam<EinsumParams> {};
@@ -68,7 +65,7 @@ PERF_TEST_P_(Layer_Einsum, einsum) {
lp.name = "testEinsum";
lp.set("equation", params.equation);
lp.set("inputSize", params.inputSize);
lp.set("outputSize", params.outputSize);
lp.set("outputSize", 1);
CV_CheckFalse(params.einsumInpShapes.empty(), "ERROR no inputs shapes provided");
@@ -79,38 +76,27 @@ PERF_TEST_P_(Layer_Einsum, einsum) {
Net net;
std::vector<Mat> inputs;
std::vector<std::string> input_names;
if (params.inputSize == 1){
int id = net.addLayer(lp.name, lp.type, lp);
for (int i = 0; i < params.inputSize; ++i) {
// create inputs
inputs.emplace_back(Mat(params.einsumInpShapes[0].size(), params.einsumInpShapes[0].data(), CV_32FC1));
inputs.emplace_back(Mat(params.einsumInpShapes[i].size(), params.einsumInpShapes[i].data(), CV_32FC1));
int id = net.addLayerToPrev(lp.name, lp.type, lp);
net.connect(0, 0, id, 0);
// connect each input to the layer
net.connect(0, i, id, i);
input_names.emplace_back("input1");
} else {
// create inputs
inputs.emplace_back(Mat(params.einsumInpShapes[0].size(), params.einsumInpShapes[0].data(), CV_32FC1));
inputs.emplace_back(Mat(params.einsumInpShapes[1].size(), params.einsumInpShapes[1].data(), CV_32FC1));
int id = net.addLayerToPrev(lp.name, lp.type, lp);
net.connect(0, 0, id, 0);
net.connect(0, 1, id, 1);
input_names.emplace_back("input1");
input_names.emplace_back("input2");
// create input names dynamically, assuming input naming follows a consistent pattern
input_names.emplace_back("input" + std::to_string(i + 1));
}
//warm up
std::vector<Mat> outputs;
net.setInputsNames(input_names);
for (int i = 0; i < input_names.size(); i++){
net.setInput(inputs[i], input_names[i]);
}
Mat out = net.forward();
net.forward(outputs, "testEinsum");
std::vector<Mat> outputs;
TEST_CYCLE()
{
net.forward(outputs, "testEinsum");