Skip to content

Commit e5b871f

Browse files
authored
Merge pull request opencv#26059 from Abdurrahheem:ash/fix-einsum-allocation
Einsum buffer allocation fix opencv#26059 This PR fixed buffer allocation issue in Einsum layer that causes segmentation fault on 32bit platforms. Related issue opencv#26008 ### 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 - [x] 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
1 parent a3bdbf5 commit e5b871f

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

modules/dnn/src/layers/einsum_layer.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ class LayerEinsumImpl CV_FINAL : public EinsumLayer
459459
{
460460
CV_TRACE_FUNCTION();
461461
CV_TRACE_ARG_VALUE(name, "name", name.c_str());
462+
CV_CheckEQ((size_t)inputs_arr.total(), (size_t)numInputs, "Number of inputs in forward and inputs during graph constructions do not match");
462463

463464
if (inputs_arr.depth() == CV_16F)
464465
{
@@ -541,7 +542,7 @@ class LayerEinsumImpl CV_FINAL : public EinsumLayer
541542
// Use either the preprocessed inputs (if it is available) or the corresponding raw inputs
542543
result = pairwiseOperandProcess(!result.empty() ? result : rawInputs[0],
543544
!result.empty() ? tmpResult : homogenizedInputDims[0],
544-
!preProcessedInputs[input].empty() ? preProcessedInputs[input] : rawInputs[input],
545+
(!preProcessedInputs[input].empty()) ? preProcessedInputs[input] : rawInputs[input],
545546
homogenizedInputDims[input],
546547
reducedDims,
547548
isFinalPair);
@@ -605,8 +606,8 @@ void LayerEinsumImpl::preProcessInputs(InputArrayOfArrays& inputs_arr)
605606
std::vector<cv::Mat> inputs;
606607
inputs_arr.getMatVector(inputs);
607608

608-
preProcessedInputs.reserve(inputs.size());
609-
homogenizedInputDims.reserve(inputs.size());
609+
preProcessedInputs.resize(inputs.size());
610+
homogenizedInputDims.resize(inputs.size());
610611

611612
int inputIter = 0;
612613
for(const Mat& input : inputs)
@@ -615,6 +616,11 @@ void LayerEinsumImpl::preProcessInputs(InputArrayOfArrays& inputs_arr)
615616

616617
// variable to hold processed version of the original input
617618
MatShape input_dims = shape(input);
619+
if (input_dims.empty()){
620+
homogenizedInputDims[inputIter] = MatShape(numLetterIndices, 1);
621+
++inputIter;
622+
continue;
623+
}
618624

619625
const auto& currSubscriptIndices = inputSubscriptIndices[inputIter];
620626

@@ -667,9 +673,9 @@ void LayerEinsumImpl::preProcessInputs(InputArrayOfArrays& inputs_arr)
667673
{
668674
preprocessed = preprocessed.reshape(1, homogenizedInputDims_.size(), homogenizedInputDims_.data());
669675
}
676+
preProcessedInputs[inputIter] = preprocessed;
677+
homogenizedInputDims[inputIter] = homogenizedInputDims_;
670678

671-
preProcessedInputs.emplace_back(preprocessed);
672-
homogenizedInputDims.emplace_back(homogenizedInputDims_);
673679
++inputIter;
674680
}
675681
}

0 commit comments

Comments
 (0)