mirror of
https://github.com/opencv/opencv.git
synced 2024-12-15 09:49:13 +08:00
ea7d4be3f8
* dnn: LSTM optimisation This uses the AVX-optimised fastGEMM1T for matrix multiplications where available, instead of the standard cv::gemm. fastGEMM1T is already used by the fully-connected layer. This commit involves two minor modifications: - Use unaligned access. I don't believe this involves any performance hit in on modern CPUs (Nehalem and Bulldozer onwards) in the case where the address is actually aligned. - Allow for weight matrices where the number of columns is not a multiple of 8. I have not enabled AVX-512 as I don't have an AVX-512 CPU to test on. * Fix warning about initialisation order * Remove C++11 syntax * Fix build when AVX(2) is not available In this case the CV_TRY_X macros are defined to 0, rather than being undefined. * Minor changes as requested: - Don't check hardware support for AVX(2) when dispatch is disabled for these - Add braces * Fix out-of-bounds access in fully connected layer The old tail handling in fastGEMM1T implicitly rounded vecsize up to the next multiple of 8, and the fully connected layer implements padding up to the next multiple of 8 to cope with this. The new tail handling does not round the vecsize upwards like this but it does require that the vecsize is at least 8. To adapt to the new tail handling, the fully connected layer now rounds vecsize itself at the same time as adding the padding(which makes more sense anyway). This also means that the fully connected layer always passes a vecsize of at least 8 to fastGEMM1T, which fixes the out-of-bounds access problems. * Improve tail mask handling - Use static array for generating tail masks (as requested) - Apply tail mask to the weights as well as the input vectors to prevent spurious propagation of NaNs/Infs * Revert whitespace change * Improve readability of conditions for using AVX * dnn(lstm): minor coding style changes, replaced left aligned load
91 lines
2.5 KiB
C++
91 lines
2.5 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));
|
|
|
|
} // namespace
|