Files
opencv-MIRROR/modules/dnn/perf/perf_recurrent.cpp
Savya Sanchi Sharma d1f25babff Merge pull request #30309 from SavyaSanchi-Sharma:opti/lstm
dnn: speed up LSTM2 (gate math, input projection, recurrence) - #30309

Speeds up the ONNX LSTM layer (`LSTM2`) in `recurrent2_layers.cpp`:
- Default sigmoid/tanh gates are computed on raw rows with the dispatched activation kernels. Peephole, clip and custom activations keep the old path.
- The input projection runs before the per-direction loop, so the GEMM uses all threads. It goes through MLAS when available.
- Batch 1 (hidden size >= 128): hidden units are split across threads, with one `parallel_for_` per timestep.
- Batch > 1: one packed GEMM per step replaces one GEMV per row.

Ryzen 9 9950X, default threads, 5.x vs this PR:

| Model | 5.x, ms | PR, ms | x-factor |
|---|:-:|:-:|:-:|
| `DNNTestNetwork.CRNN` | 2.693 | 1.562 | 1.72 |
| CRNN EN 2021sep | 2.498 | 1.472 | 1.70 |
| crnn.onnx | 2.650 | 1.434 | 1.85 |
| LSTM2 bidirectional, batch 1, in 512, hidden 256, 24 steps (CRNN layer 1) | 0.710 | 0.212 | 3.36 
### 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
- [ ] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
2026-10-19 09:41:52 +03:00

136 lines
3.8 KiB
C++

// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "perf_precomp.hpp"
namespace opencv_test {
struct LstmParams {
// Batch size
int nrSamples;
// Size of the input vector
int inputSize;
// Size of the internal state vector
int hiddenSize;
// Number of timesteps for the LSTM
int nrSteps;
};
static inline void PrintTo(const LstmParams& params, ::std::ostream* os) {
(*os) << "BATCH=" << params.nrSamples
<< ", IN=" << params.inputSize
<< ", HIDDEN=" << params.hiddenSize
<< ", TS=" << params.nrSteps;
}
static const LstmParams testLstmConfigs[] = {
{1, 192, 192, 100},
{1, 1024, 192, 100},
{1, 64, 192, 100},
{1, 192, 512, 100},
{64, 192, 192, 2},
{64, 1024, 192, 2},
{64, 64, 192, 2},
{64, 192, 512, 2},
{128, 192, 192, 2},
{128, 1024, 192, 2},
{128, 64, 192, 2},
{128, 192, 512, 2}
};
class Layer_LSTM : public TestBaseWithParam<LstmParams> {};
PERF_TEST_P_(Layer_LSTM, lstm) {
const LstmParams& params = GetParam();
LayerParams lp;
lp.type = "LSTM";
lp.name = "testLstm";
lp.set("produce_cell_output", false);
lp.set("use_timestamp_dim", true);
Mat weightH(params.hiddenSize * 4, params.hiddenSize, CV_32FC1, cv::Scalar(0));
Mat weightX(params.hiddenSize * 4, params.inputSize, CV_32FC1, cv::Scalar(0));
Mat bias(params.hiddenSize * 4, 1, CV_32FC1, cv::Scalar(0));
Mat hInternal(params.nrSteps, params.hiddenSize, CV_32FC1, cv::Scalar(0));
Mat cInternal(params.nrSteps, params.hiddenSize, CV_32FC1, cv::Scalar(0));
lp.blobs.push_back(weightH);
lp.blobs.push_back(weightX);
lp.blobs.push_back(bias);
lp.blobs.push_back(hInternal);
lp.blobs.push_back(cInternal);
std::vector<int> inputDims;
inputDims.push_back(params.nrSamples);
inputDims.push_back(params.nrSteps);
inputDims.push_back(params.inputSize);
Mat input(inputDims.size(), inputDims.data(), CV_32FC1);
input = cv::Scalar(0);
Net net;
net.addLayerToPrev(lp.name, lp.type, lp);
net.setInput(input);
// Warm up
std::vector<Mat> outputs(2);
net.forward(outputs, "testLstm");
TEST_CYCLE()
{
net.forward(outputs, "testLstm");
}
SANITY_CHECK_NOTHING();
}
INSTANTIATE_TEST_CASE_P(/**/, Layer_LSTM, testing::ValuesIn(testLstmConfigs));
class Layer_LSTM2 : public TestBaseWithParam<LstmParams> {};
PERF_TEST_P_(Layer_LSTM2, lstm2) {
const LstmParams& params = GetParam();
const int H = params.hiddenSize;
LayerParams lp;
lp.type = "LSTM2";
lp.name = "testLstm2";
lp.set("hidden_size", H);
lp.set("const_weights", true);
// ONNX layout: X is (seq, batch, input), W/R/B are passed as inputs like the ONNX importer does
Mat x({params.nrSteps, params.nrSamples, params.inputSize}, CV_32F);
Mat W({1, 4 * H, params.inputSize}, CV_32F);
Mat R({1, 4 * H, H}, CV_32F);
Mat B({1, 8 * H}, CV_32F);
randu(x, -1.f, 1.f);
randu(W, -0.1f, 0.1f);
randu(R, -0.1f, 0.1f);
randu(B, -0.1f, 0.1f);
Net net;
int id = net.addLayer(lp.name, lp.type, lp);
std::vector<String> inputNames{"x", "W", "R", "B"};
for (int i = 0; i < (int)inputNames.size(); i++)
net.connect(0, i, id, i);
net.setInputsNames(inputNames);
net.setInput(x, "x");
net.setInput(W, "W");
net.setInput(R, "R");
net.setInput(B, "B");
// Warm up
std::vector<Mat> outputs;
net.forward(outputs, "testLstm2");
TEST_CYCLE()
{
net.forward(outputs, "testLstm2");
}
SANITY_CHECK_NOTHING();
}
INSTANTIATE_TEST_CASE_P(/**/, Layer_LSTM2, testing::ValuesIn(testLstmConfigs));
} // namespace