[CPU] Dynamic shapes. RNN node. (#9002)

This commit is contained in:
Nikolay Shchegolev authored and GitHub committed 2022-01-11 18:17:35 +03:00
1 parent 02cabcda3c
commit 62e8923605
17 files changed
+1704 -1005

No files matched your search

+31 -7
View File
@@ -17,11 +17,11 @@ class Shape {
public:
Shape() = default;
explicit Shape(const ngraph::PartialShape& shape) {
explicit Shape(const ov::PartialShape& shape) {
minDims = shape.get_min_shape();
std::transform(minDims.begin(), minDims.end(), minDims.begin(), [](Dim x){ return ngraph::Interval::s_max == x ? UNDEFINED_DIM : x;});
std::transform(minDims.begin(), minDims.end(), minDims.begin(), [](Dim x){ return ov::Interval::s_max == x ? UNDEFINED_DIM : x;});
maxDims = shape.get_max_shape();
std::transform(maxDims.begin(), maxDims.end(), maxDims.begin(), [](Dim x){ return ngraph::Interval::s_max == x ? UNDEFINED_DIM : x;});
std::transform(maxDims.begin(), maxDims.end(), maxDims.begin(), [](Dim x){ return ov::Interval::s_max == x ? UNDEFINED_DIM : x;});
type = shape.is_static() ? ShapeType::Static : ShapeType::Dynamic;
initDims();
@@ -29,7 +29,7 @@ public:
hasZeroDimensions = std::any_of(dims.begin(), dims.end(), [](size_t dim) { return dim == 0; } );
}
explicit Shape(const InferenceEngine::SizeVector& shape) {
explicit Shape(const VectorDims& shape) {
minDims = shape;
maxDims = shape;
type = ShapeType::Static;
@@ -39,6 +39,30 @@ public:
hasZeroDimensions = std::any_of(dims.begin(), dims.end(), [](size_t dim) { return dim == 0; } );
}
Shape(const VectorDims& minVals, const VectorDims& maxVals) {
minDims = minVals;
maxDims = maxVals;
type = minVals == maxVals ? ShapeType::Static : ShapeType::Dynamic;
initDims();
hasZeroDimensions = std::any_of(dims.begin(), dims.end(), [](size_t dim) { return dim == 0; } );
}
Shape(const std::initializer_list<Dim>& shape) {
minDims.reserve(shape.size());
maxDims.reserve(shape.size());
type = ShapeType::Static;
for (auto dim : shape) {
minDims.push_back(dim);
maxDims.push_back(dim);
}
initDims();
hasZeroDimensions = std::any_of(dims.begin(), dims.end(), [](size_t dim) { return dim == 0; } );
}
/**
* @brief
* for static shape
@@ -132,8 +156,8 @@ public:
return size;
}
ngraph::PartialShape toPartialShape() const {
using ngraph::Dimension;
ov::PartialShape toPartialShape() const {
using ov::Dimension;
std::vector<Dimension> nGraphDims;
nGraphDims.reserve(minDims.size());
for (int i = 0; i < minDims.size(); i++) {
@@ -141,7 +165,7 @@ public:
Dimension::value_type maxDim = Shape::UNDEFINED_DIM == maxDims[i] ? -1 : maxDims[i];
nGraphDims.emplace_back(minDim, maxDim);
}
return ngraph::PartialShape(nGraphDims);
return ov::PartialShape(nGraphDims);
}
bool isCompatible(const VectorDims& vecDims) const;
@@ -2022,10 +2022,11 @@ void MKLDNNGraphOptimizer::reshapeRnnSeq(MKLDNNGraph &graph) {
}
auto childrenEdges = parentNode->getChildEdgesAtPort(0);
std::vector<ov::Dimension> origShape = static_cast<std::vector<ov::Dimension>>(parentNode->getOutputShapeAtPort(0).toPartialShape());
origShape.erase(origShape.begin() + 1);
const auto newShape = Shape(origShape);
parentNode->outputShapes[0] = newShape;
auto minDims = parentNode->getOutputShapeAtPort(0).getMinDims();
auto maxDims = parentNode->getOutputShapeAtPort(0).getMaxDims();
minDims.erase(minDims.begin() + 1);
maxDims.erase(maxDims.begin() + 1);
parentNode->outputShapes[0] = {minDims, maxDims};
for (size_t j = 0; j < childrenEdges.size(); j++) {
auto edge = childrenEdges[j];
+4 -3
View File
@@ -648,8 +648,7 @@ void MKLDNNNode::filterSupportedPrimitiveDescriptors() {
}
void MKLDNNNode::initDescriptor(const NodeConfig& config) {
auto* selectedPD = getSelectedPrimitiveDescriptor();
if (!selectedPD) {
if (!getSelectedPrimitiveDescriptor()) {
return;
}
std::vector<MemoryDescPtr> inDescs;
@@ -662,6 +661,7 @@ void MKLDNNNode::initDescriptor(const NodeConfig& config) {
AttrPtr attr = initPrimitiveAttr();
auto* selectedPD = getSelectedPrimitiveDescriptor();
NodeConfig rightConfig = selectedPD->getConfig();
size_t selected_count = 0;
for (size_t j = 0; j < descs.size(); j++) {
@@ -1376,8 +1376,9 @@ bool MKLDNNNode::hasEmptyOutputTensors() const {
bool MKLDNNNode::inputShapesDefined() const {
for (size_t i = 0; i < getParentEdges().size(); i++) {
if (!getParentEdgesAtPort(i)[0]->getMemory().getDesc().isDefined())
if (!getParentEdgesAtPort(i)[0]->getMemory().getDesc().isDefined()) {
return false;
}
}
return true;
}
@@ -39,7 +39,7 @@ private:
NUMPY,
EXPLICIT
};
AutoBroadcastType broadcastType;
AutoBroadcastType broadcastType = NUMPY;
static constexpr size_t INPUT_DATA_IDX = 0;
static constexpr size_t TARGET_SHAPE_IDX = 1;
@@ -907,7 +907,6 @@ std::shared_ptr<MKLDNNDescriptor> MKLDNNConvolutionNode::createMkldnnConvDesc(co
const mkldnn::memory::desc& wghDesc,
const mkldnn::memory::desc& dstDesc,
const mkldnn::memory::desc& biasDesc) {
std::shared_ptr<mkldnn::convolution_forward::desc> dnnlConvDesc;
auto alg = isWinograd() ? mkldnn::algorithm::convolution_winograd : mkldnn::algorithm::convolution_direct;
if (withBiases) {
@@ -931,11 +930,11 @@ void MKLDNNConvolutionNode::prepareParams() {
auto wghMemPtr = getParentEdgesAtPort(1)[0]->getMemoryPtr();
auto dstMemPtr = getChildEdgesAtPort(0)[0]->getMemoryPtr();
if (!dstMemPtr || !dstMemPtr->GetPrimitivePtr())
IE_THROW() << "Destination memory didn't allocate.";
IE_THROW() << "Destination memory was not allocated.";
if (!srcMemPtr || !srcMemPtr->GetPrimitivePtr())
IE_THROW() << "Input memory didn't allocate.";
IE_THROW() << "Input memory was not allocated.";
if (!wghMemPtr || !wghMemPtr->GetPrimitivePtr())
IE_THROW() << "Weight memory didn't allocate.";
IE_THROW() << "Weight memory was not allocated.";
MKLDNNMemoryPtr biasMemPtr = nullptr;
if (withBiases) {
biasMemPtr = getParentEdgesAtPort(2)[0]->getMemoryPtr();
File diff suppressed because it is too large. Load diff
+38 -15
View File
@@ -5,10 +5,10 @@
#pragma once
#include <mkldnn_node.h>
#include <string>
#include <memory>
#include <vector>
#include "memory_desc/dnnl_blocked_memory_desc.h"
namespace MKLDNNPlugin {
@@ -18,7 +18,6 @@ public:
static bool isSupportedOperation(const std::shared_ptr<const ngraph::Node>& op, std::string& errorMessage) noexcept;
void getSupportedDescriptors() override;
void createPrimitive() override;
std::shared_ptr<MemoryDesc> getSrcMemDesc(mkldnn::primitive_desc_iterator& primitive_desc_it, size_t idx) override;
std::shared_ptr<MemoryDesc> getDstMemDesc(mkldnn::primitive_desc_iterator& primitive_desc_it, size_t idx) override;
bool created() const override;
@@ -31,11 +30,19 @@ public:
return nativeOrder;
}
void cleanup() override;
protected:
std::vector<VectorDims> shapeInfer() const override;
void prepareParams() override;
void executeDynamicImpl(mkldnn::stream strm) override;
private:
void initCell(const std::shared_ptr<ngraph::Node>& op);
void initSeq(const std::shared_ptr<ngraph::Node>& op);
void initCell();
void initSequence();
void fillCellDesc();
void fillSeqDesc();
void fillSequenceDesc();
void fillDescs();
bool verifyWeightsPrecision(const InferenceEngine::Precision& layerPrec,
const InferenceEngine::Precision& weightsPrec);
@@ -46,8 +53,6 @@ private:
void copyWeightsData();
private:
InferenceEngine::Precision runtimePrecision;
/** Specify mode Cell or Seq. true - Cell, false - Seq */
bool is_cell = false;
@@ -64,11 +69,26 @@ private:
mkldnn::algorithm cell_act = mkldnn::algorithm::eltwise_tanh;
/** Weights data and state memory format: ldigo or any */
mkldnn::memory::format_tag w_format = mkldnn::memory::format_tag::any;
mkldnn::memory::format_tag wFormat = mkldnn::memory::format_tag::any;
struct Interval {
Interval() = default;
Interval(Dim min, Dim max) {
minVal = min;
maxVal = max;
}
bool isStatic() {
return minVal == maxVal;
}
Dim minVal = 0;
Dim maxVal = 0;
};
// Internal attributes
size_t N = 0; /**< Batch value */
size_t T = 0; /**< Sequence value */
Interval N; /**< Batch value */
Interval T; /**< Sequence value */
size_t DC = 0; /**< Input data channel size */
size_t SC = 0; /**< State channel size value */
size_t G = 0; /**< Gate size. LSTM - 4, GRU - 3, RNN - 1 */
@@ -77,8 +97,9 @@ private:
const size_t L = 1; /**< What is it??. Constant for mkldnn impl */
const size_t D = 1; /**< Num of direction. 1 or 2 */
std::vector<DnnlBlockedMemoryDesc> in_data_d;
std::vector<DnnlBlockedMemoryDesc> out_data_d;
std::vector<DnnlBlockedMemoryDesc> inDataDescs;
std::vector<DnnlBlockedMemoryDesc> outDataDescs;
std::vector<mkldnn::memory::desc> wDescs;
enum RNNInOutKind {
Layer = 0,
@@ -86,14 +107,16 @@ private:
CellState = 2
};
std::vector<size_t > in_data_dims;
std::vector<size_t > out_data_dims;
size_t wIdx = 0;
size_t rIdx = 0;
size_t bIdx = 0;
static const std::map<InferenceEngine::Precision, InferenceEngine::Precision> weightsByLayerPrec;
static constexpr size_t optimalBatchSize = 16lu;
static constexpr size_t batchDimDummyValue = 64lu;
bool wasMemoryPrepared = false;
};
} // namespace MKLDNNPlugin