perf_einsum.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include "perf_precomp.hpp"
  5. namespace opencv_test {
  6. struct EinsumParams {
  7. int inputSize;
  8. int outputSize;
  9. std::string equation;
  10. std::vector<MatShape> einsumInpShapes;
  11. EinsumParams(std::string equation_, std::vector<MatShape> einsumInpShapes_ = std::vector<MatShape>())
  12. {
  13. inputSize = einsumInpShapes_.size();
  14. equation = equation_;
  15. einsumInpShapes = einsumInpShapes_;
  16. }
  17. };
  18. static inline void PrintTo(const EinsumParams& params, ::std::ostream* os) {
  19. (*os) << "Equation=" << params.equation << " ";
  20. (*os) << "InputShape={";
  21. for(int i = 0; i < params.einsumInpShapes.size(); i++)
  22. {
  23. (*os) << "{";
  24. for(int j = 0; j < params.einsumInpShapes[i].size(); j++)
  25. {
  26. (*os) << params.einsumInpShapes[i][j] << ((j < params.einsumInpShapes[i].size() - 1) ? ", " : "");
  27. }
  28. (*os) << ((i < params.einsumInpShapes.size() - 1) ? "}, " : "}");
  29. }
  30. (*os) << "}";
  31. }
  32. // test cases
  33. static const EinsumParams testEinsumConfigs[] = {
  34. // TODO: Add tests with one input after ellips merge
  35. {"ij, jk -> ik", {{2, 3}, {3, 2}}},
  36. {"ij, jk -> ik", {{20, 30}, {30, 20}}},
  37. {"ij, jk -> ik", {{113, 127}, {127, 113}}},
  38. {"imkj, injs -> imnks", {{1, 4, 7, 9}, {1, 5, 9, 8}}},
  39. {"imkj, injs -> imnks", {{1, 4, 70, 90}, {1, 5, 90, 80}}},
  40. {"imkj, injs -> imnks", {{1, 4, 73, 91}, {1, 5, 91, 57}}},
  41. {"ij -> i", {{30, 40}}},
  42. {"ij -> i", {{113, 374}}},
  43. {"...ij -> ...i", {{30, 40}}},
  44. {"...ij -> ...i", {{113, 374}}},
  45. {"...ij, ...jk -> ...ik", {{40, 50}, {50, 80}}},
  46. {"...ij, ...jk -> ...ik", {{47, 51}, {51, 83}}},
  47. };
  48. class Layer_Einsum: public TestBaseWithParam<EinsumParams> {};
  49. PERF_TEST_P_(Layer_Einsum, einsum) {
  50. const EinsumParams& params = GetParam();
  51. LayerParams lp;
  52. lp.type = "Einsum";
  53. lp.name = "testEinsum";
  54. lp.set("equation", params.equation);
  55. lp.set("inputSize", params.inputSize);
  56. lp.set("outputSize", 1);
  57. CV_CheckFalse(params.einsumInpShapes.empty(), "ERROR no inputs shapes provided");
  58. for (int i = 0; i < params.einsumInpShapes.size(); i++) {
  59. lp.set("inputShapes" + cv::format("%d", i), DictValue::arrayInt(params.einsumInpShapes[i].begin(), params.einsumInpShapes[i].size()));
  60. }
  61. Net net;
  62. std::vector<Mat> inputs;
  63. std::vector<std::string> input_names;
  64. int id = net.addLayer(lp.name, lp.type, lp);
  65. for (int i = 0; i < params.inputSize; ++i) {
  66. // create inputs
  67. inputs.emplace_back(Mat(params.einsumInpShapes[i].size(), params.einsumInpShapes[i].data(), CV_32FC1));
  68. // connect each input to the layer
  69. net.connect(0, i, id, i);
  70. // create input names dynamically, assuming input naming follows a consistent pattern
  71. input_names.emplace_back("input" + std::to_string(i + 1));
  72. }
  73. //warm up
  74. std::vector<Mat> outputs;
  75. net.setInputsNames(input_names);
  76. for (int i = 0; i < input_names.size(); i++){
  77. net.setInput(inputs[i], input_names[i]);
  78. }
  79. net.forward(outputs, "testEinsum");
  80. TEST_CYCLE()
  81. {
  82. net.forward(outputs, "testEinsum");
  83. }
  84. SANITY_CHECK_NOTHING();
  85. }
  86. INSTANTIATE_TEST_CASE_P(/**/, Layer_Einsum, testing::ValuesIn(testEinsumConfigs));
  87. }; //namespace