Skip to content

Commit

Permalink
Cannot dynamically cast MemoryDesc fix (#6)
Browse files Browse the repository at this point in the history
* Cannot dynamically cast MemoryDesc fix

* qew

* review fixes

* Revert "qew"

This reverts commit 6c907a6.
  • Loading branch information
Maxim Andronov committed Jul 20, 2021
1 parent 0edf5e8 commit f38c336
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 86 deletions.
8 changes: 1 addition & 7 deletions inference-engine/src/mkldnn_plugin/cpu_memory_desc_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,8 @@ class MemoryDescUtils {
static MKLDNNMemoryDesc convertToMKLDNNMemoryDesc(const MemoryDesc& desc);
static MKLDNNMemoryDesc convertToMKLDNNMemoryDesc(const BlockedMemoryDesc& desc);
static MKLDNNMemoryDesc convertToMKLDNNMemoryDesc(const InferenceEngine::TensorDesc& desc);

private:
static BlockedMemoryDesc convertToBlockedDescriptor(const MKLDNNMemoryDesc& inpDesc);
static BlockedMemoryDesc convertToBlockedDescriptor(const MemoryDesc& desc);

friend class MKLDNNMemory;
friend class MKLDNNGraphOptimizer;

static BlockedMemoryDesc convertToBlockedDescriptor(const MKLDNNMemoryDesc& inpDesc);
//static MemoryDescPtr getUndefinedMemoryDesc(const MKLDNNMemoryDesc& desc);
};

Expand Down
48 changes: 24 additions & 24 deletions inference-engine/src/mkldnn_plugin/nodes/mkldnn_concat_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <limits>
#include "common/cpu_memcpy.h"
#include "common/blocked_desc_creator.h"
#include <cpu_memory_desc_utils.h>

using namespace mkldnn;
using namespace MKLDNNPlugin;
Expand Down Expand Up @@ -162,8 +163,8 @@ void MKLDNNConcatNode::initSupportedPrimitiveDescriptors() {
const auto& refConfig = supportedPrimitiveDescriptors[refPdIndex].getConfig();
auto config = refConfig;

const auto& order = (refConfig.outConfs[0].desc->as<BlockedMemoryDesc>())->getOrder();
const auto& blkDims = (refConfig.outConfs[0].desc->as<BlockedMemoryDesc>())->getBlockDims();
const auto &order = refConfig.outConfs[0].desc->as<BlockedMemoryDesc>()->getOrder();
const auto &blkDims = refConfig.outConfs[0].desc->as<BlockedMemoryDesc>()->getBlockDims();
auto numOfDim = blkDims.size();

SizeVector offsets(numOfDim, 0lu);
Expand All @@ -182,7 +183,7 @@ void MKLDNNConcatNode::initSupportedPrimitiveDescriptors() {
config.outConfs[0].desc = make_unique<BlockedMemoryDesc>(outputPrecision, dstDims, blkDims, order, offset, offsets, strides);

for (size_t i = 0; i < getParentEdges().size(); i++) {
const auto& srcBlkDims = (refConfig.inConfs[i].desc->as<BlockedMemoryDesc>())->getBlockDims();
const auto& srcBlkDims = refConfig.inConfs[i].desc->as<BlockedMemoryDesc>()->getBlockDims();
const auto& dims = refConfig.inConfs[i].desc->getShape().getStaticDims();

config.inConfs[i].inPlace = 0;
Expand Down Expand Up @@ -433,36 +434,35 @@ void MKLDNNConcatNode::initOptimalPrimitiveDescriptor() {
}

// reset undefined offsets
auto outBlockingDesc = config.outConfs[i].desc->as<const BlockedMemoryDesc>();
config.outConfs[i].desc = make_unique<BlockedMemoryDesc>(outBlockingDesc->getPrecision(), outBlockingDesc->getShape().getStaticDims(),
outBlockingDesc->getBlockDims(), outBlockingDesc->getOrder());
auto outBlockingDesc = MemoryDescUtils::convertToBlockedDescriptor(*config.outConfs[i].desc);
config.outConfs[i].desc = make_unique<BlockedMemoryDesc>(outBlockingDesc.getPrecision(), outBlockingDesc.getShape().getStaticDims(),
outBlockingDesc.getBlockDims(), outBlockingDesc.getOrder());
}
auto firstOutBlockingDesc = config.outConfs[0].desc->as<const BlockedMemoryDesc>();
auto firstOutBlockingDesc = MemoryDescUtils::convertToBlockedDescriptor(*config.outConfs[0].desc);
size_t offset = 0;
for (size_t i = 0; i < config.inConfs.size(); i++) {
auto inpDesc = config.inConfs[i].desc->clone();
auto inpBlockingDesc = inpDesc->as<const BlockedMemoryDesc>();
config.inConfs[i].desc = make_unique<BlockedMemoryDesc>(inpBlockingDesc->getPrecision(),
inpBlockingDesc->getShape().getStaticDims(),
inpBlockingDesc->getBlockDims(),
inpBlockingDesc->getOrder(),
firstOutBlockingDesc->getOffsetPadding() + offset,
firstOutBlockingDesc->getOffsetPaddingToData(),
firstOutBlockingDesc->getStrides());
auto inpBlockingDesc = MemoryDescUtils::convertToBlockedDescriptor(*config.inConfs[i].desc);
config.inConfs[i].desc = make_unique<BlockedMemoryDesc>(inpBlockingDesc.getPrecision(),
inpBlockingDesc.getShape().getStaticDims(),
inpBlockingDesc.getBlockDims(),
inpBlockingDesc.getOrder(),
firstOutBlockingDesc.getOffsetPadding() + offset,
firstOutBlockingDesc.getOffsetPaddingToData(),
firstOutBlockingDesc.getStrides());
size_t axisSize = 1;

auto firstInpBlockingDesc = config.inConfs[0].desc->as<BlockedMemoryDesc>();
if (firstInpBlockingDesc->checkGeneralLayout(GeneralLayout::nspc)) {
auto firstInpBlockingDesc = MemoryDescUtils::convertToBlockedDescriptor(*config.inConfs[0].desc);
if (firstInpBlockingDesc.checkGeneralLayout(GeneralLayout::nspc)) {
// This is more general and works for any "direct" Layout (such as nchw or nhwc), but it doesn't work for blocked
size_t realAxis = inverseOrder(firstInpBlockingDesc->getOrder(), axis);
for (size_t j = realAxis; j < inpBlockingDesc->getBlockDims().size(); j++) {
size_t jj = firstInpBlockingDesc->getOrder()[j];
axisSize *= inpBlockingDesc->getBlockDims()[jj];
size_t realAxis = inverseOrder(firstInpBlockingDesc.getOrder(), axis);
for (size_t j = realAxis; j < inpBlockingDesc.getBlockDims().size(); j++) {
size_t jj = firstInpBlockingDesc.getOrder()[j];
axisSize *= inpBlockingDesc.getBlockDims()[jj];
}
} else {
// This works for nchw and nchw8c/nchw16c
for (size_t j = axis; j < inpBlockingDesc->getBlockDims().size(); j++) {
axisSize *= inpBlockingDesc->getBlockDims()[j];
for (size_t j = axis; j < inpBlockingDesc.getBlockDims().size(); j++) {
axisSize *= inpBlockingDesc.getBlockDims()[j];
}
}
offset += axisSize;
Expand Down
13 changes: 7 additions & 6 deletions inference-engine/src/mkldnn_plugin/nodes/mkldnn_conv_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <ngraph/ops.hpp>
#include <cpu/x64/jit_generator.hpp>
#include "common/cpu_convert.h"
#include <cpu_memory_desc_utils.h>

using namespace mkldnn;
using namespace MKLDNNPlugin;
Expand Down Expand Up @@ -480,13 +481,13 @@ bool MKLDNNConvolutionNode::created() const {

void MKLDNNConvolutionNode::createDescriptor(const std::vector<const MemoryDesc*>& inputDesc,
const std::vector<const MemoryDesc*>& outputDesc) {
auto inDesc = inputDesc[0]->as<MKLDNNMemoryDesc>();
auto outDesc = outputDesc[0]->as<MKLDNNMemoryDesc>();
auto inDesc = MemoryDescUtils::convertToMKLDNNMemoryDesc(*inputDesc[0]);
auto outDesc = MemoryDescUtils::convertToMKLDNNMemoryDesc(*outputDesc[0]);

memory::data_type wdt = MKLDNNExtensionUtils::IEPrecisionToDataType(inDesc->getPrecision());
memory::data_type wdt = MKLDNNExtensionUtils::IEPrecisionToDataType(inDesc.getPrecision());
memory::data_type bdt = memory::data_type::f32;

if (inDesc->getPrecision() == Precision::U8 || inDesc->getPrecision() == Precision::I8) {
if (inDesc.getPrecision() == Precision::U8 || inDesc.getPrecision() == Precision::I8) {
wdt = memory::data_type::s8;
}

Expand All @@ -507,14 +508,14 @@ void MKLDNNConvolutionNode::createDescriptor(const std::vector<const MemoryDesc*
mkldnn::memory::desc bias_candidate(blocked_biasesDims, bdt, memory::format_tag::any);

conv_desc.reset(new convolution_forward::desc(prop_kind::forward_scoring, alg,
*inDesc, wgh_candidate, bias_candidate, *outDesc,
inDesc, wgh_candidate, bias_candidate, outDesc,
mkldnn::memory::dims(stride.begin(), stride.end()),
mkldnn::memory::dims(dilation.begin(), dilation.end()),
mkldnn::memory::dims(paddingL.begin(), paddingL.end()),
mkldnn::memory::dims(paddingR.begin(), paddingR.end())));
} else {
conv_desc.reset(new convolution_forward::desc(prop_kind::forward_scoring, alg,
*inDesc, wgh_candidate, *outDesc,
inDesc, wgh_candidate, outDesc,
mkldnn::memory::dims(stride.begin(), stride.end()),
mkldnn::memory::dims(dilation.begin(), dilation.end()),
mkldnn::memory::dims(paddingL.begin(), paddingL.end()),
Expand Down
17 changes: 9 additions & 8 deletions inference-engine/src/mkldnn_plugin/nodes/mkldnn_deconv_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <ngraph/opsets/opset1.hpp>
#include <cpu/x64/cpu_isa_traits.hpp>
#include <nodes/common/cpu_memcpy.h>
#include <cpu_memory_desc_utils.h>

using namespace mkldnn;
using namespace MKLDNNPlugin;
Expand Down Expand Up @@ -348,11 +349,11 @@ void MKLDNNDeconvolutionNode::createPrimitive() {

void MKLDNNDeconvolutionNode::createDescriptor(const std::vector<const MemoryDesc*> &inputDesc,
const std::vector<const MemoryDesc*> &outputDesc) {
const MKLDNNMemoryDesc* in_candidate = inputDesc[0]->as<MKLDNNMemoryDesc>();
const MKLDNNMemoryDesc* out_candidate = outputDesc[0]->as<MKLDNNMemoryDesc>();;
const MKLDNNMemoryDesc in_candidate = MemoryDescUtils::convertToMKLDNNMemoryDesc(*inputDesc[0]);
const MKLDNNMemoryDesc out_candidate = MemoryDescUtils::convertToMKLDNNMemoryDesc(*outputDesc[0]);

// grouping and autoblicking is not compatible
if ((withGroups && !isDW) && (in_candidate->blocksExtended() || out_candidate->blocksExtended()))
if ((withGroups && !isDW) && (in_candidate.blocksExtended() || out_candidate.blocksExtended()))
return;

auto convertDims = [] (const std::vector<ptrdiff_t>& orig_dims) {
Expand All @@ -364,25 +365,25 @@ void MKLDNNDeconvolutionNode::createDescriptor(const std::vector<const MemoryDes
mkldnn::memory::desc wgh_candidate(weightsDims, memory::data_type::s8, memory::format_tag::any);
std::shared_ptr<mkldnn::deconvolution_forward::desc> deconv_desc;
deconv_desc.reset(new deconvolution_forward::desc(prop_kind::forward_inference, mkldnn::algorithm::deconvolution_direct,
*in_candidate, wgh_candidate, *out_candidate,
in_candidate, wgh_candidate, out_candidate,
convertDims(stride), convertDims(dilation),
convertDims(paddingL), convertDims(paddingR)));
descs.emplace_back(deconv_desc);
} else {
MKLDNNDims weightsDims = MKLDNNDims(weightDims);
mkldnn::memory::desc wgh_candidate(weightsDims, in_candidate->getDataType(), memory::format_tag::any);
mkldnn::memory::desc wgh_candidate(weightsDims, in_candidate.getDataType(), memory::format_tag::any);
for (auto alg : {mkldnn::algorithm::convolution_winograd, mkldnn::algorithm::convolution_direct}) {
std::shared_ptr<mkldnn::convolution_forward::desc> conv_desc;
conv_desc.reset(new convolution_forward::desc(prop_kind::forward_inference, alg,
*out_candidate, wgh_candidate, *in_candidate,
out_candidate, wgh_candidate, in_candidate,
convertDims(stride),
convertDims(dilation),
convertDims(paddingL),
convertDims(paddingR)));

std::shared_ptr<mkldnn::convolution_backward_data::desc> deconv_desc;
deconv_desc.reset(new convolution_backward_data::desc(alg, *out_candidate, wgh_candidate,
*in_candidate,
deconv_desc.reset(new convolution_backward_data::desc(alg, out_candidate, wgh_candidate,
in_candidate,
convertDims(stride),
convertDims(dilation),
convertDims(paddingL),
Expand Down
11 changes: 5 additions & 6 deletions inference-engine/src/mkldnn_plugin/nodes/mkldnn_eltwise_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,11 @@ struct jit_uni_eltwise_generic : public MKLDNNPlugin::jit_uni_eltwise_kernel, pu
if (eltwiseNode.getFusedWith()[i].get()->getType() == Eltwise) {
post_op_emitters.push_back(create_eltwise_emitter(*eltwiseNode.getFusedWith()[i].get(), exec_prc));
} else if (eltwiseNode.getFusedWith()[i].get()->getType() == FakeQuantize) {
IE_THROW() << "[DS] Unimplemented";
// auto fakeQuantizeNode = dynamic_cast<MKLDNNFakeQuantizeNode*>(eltwiseNode.getFusedWith()[i].get());
// fakeQuantizeNode->appendPostOps(post_ops);
//
// quantization_injectors.push_back(std::make_shared<jit_uni_quantization_injector_f32<isa>>(
// this, post_ops.get()->entry_[post_ops.len() - 1], vmm_d_weights, vmm_d_bias, reg_d_weights, reg_d_bias));
auto fakeQuantizeNode = dynamic_cast<MKLDNNFakeQuantizeNode*>(eltwiseNode.getFusedWith()[i].get());
fakeQuantizeNode->appendPostOps(post_ops);

quantization_injectors.push_back(std::make_shared<jit_uni_quantization_injector_f32<isa>>(
this, post_ops.get()->entry_[post_ops.len() - 1], vmm_d_weights, vmm_d_bias, reg_d_weights, reg_d_bias));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "ie_parallel.hpp"

#include <ngraph/opsets/opset1.hpp>
#include <cpu_memory_desc_utils.h>

// Quantization ranges validation is switched off by default in order to avoid regressions on user side
// #define VALIDATE_QUANTIZATION_RANGES
Expand Down Expand Up @@ -1220,13 +1221,13 @@ void MKLDNNFakeQuantizeNode::createPrimitive() {
jqp.wei_prc = Precision::FP32;
jqp.dst_prc = config.outConfs[0].desc->getPrecision();

auto srcDesc = config.inConfs[0].desc->as<BlockedMemoryDesc>();
jqp.s_str = srcDesc->getStrides();
auto srcDesc = getParentEdgeAt(0)->getMemory().GetDescWithType<BlockedMemoryDesc>();
jqp.s_str = srcDesc.getStrides();

auto dstDesc = config.outConfs[0].desc->as<BlockedMemoryDesc>();
jqp.d_str = dstDesc->getStrides();
auto dstDesc = getChildEdgeAt(0)->getMemory().GetDescWithType<BlockedMemoryDesc>();
jqp.d_str = dstDesc.getStrides();

jqp.is_planar = srcDesc->checkGeneralLayout(GeneralLayout::ncsp) && one_of(srcDesc->getShape().getRank(), 3, 4, 5);
jqp.is_planar = srcDesc.checkGeneralLayout(GeneralLayout::ncsp) && one_of(srcDesc.getShape().getRank(), 3, 4, 5);

jqp.op_type = getAlgorithm();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <mkldnn_extension_utils.h>
#include <mkldnn.hpp>
#include "utils/general_utils.h"
#include <cpu_memory_desc_utils.h>

using namespace mkldnn;
using namespace MKLDNNPlugin;
Expand Down Expand Up @@ -283,7 +284,7 @@ void MKLDNNFullyConnectedNode::createDescriptorInternal(const mkldnn::memory::de

void MKLDNNFullyConnectedNode::createDescriptor(const std::vector<const MemoryDesc*> &inputDesc,
const std::vector<const MemoryDesc*> &outputDesc) {
createDescriptorInternal(*inputDesc[0]->as<MKLDNNMemoryDesc>(), *outputDesc[0]->as<MKLDNNMemoryDesc>());
createDescriptorInternal(MemoryDescUtils::convertToMKLDNNMemoryDesc(*inputDesc[0]), MemoryDescUtils::convertToMKLDNNMemoryDesc(*outputDesc[0]));
}

std::unique_ptr<MKLDNNMemoryDesc> MKLDNNFullyConnectedNode::getSrcMemDesc(mkldnn::primitive_desc_iterator &primitive_desc_it, size_t idx) {
Expand Down
3 changes: 2 additions & 1 deletion inference-engine/src/mkldnn_plugin/nodes/mkldnn_lrn_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <string>
#include <mkldnn_extension_utils.h>
#include <ngraph/opsets/opset1.hpp>
#include <cpu_memory_desc_utils.h>

using namespace MKLDNNPlugin;
using namespace InferenceEngine;
Expand Down Expand Up @@ -128,7 +129,7 @@ void MKLDNNLrnNode::createDescriptor(const std::vector<const MemoryDesc*> &input
const std::vector<const MemoryDesc*> &outputDesc) {
mkldnn::algorithm alg = isAcrossMaps ? mkldnn::algorithm::lrn_across_channels : mkldnn::algorithm::lrn_within_channel;
MKLDNNDescriptor desc(std::shared_ptr<mkldnn::lrn_forward::desc>(
new mkldnn::lrn_forward::desc(mkldnn::prop_kind::forward_scoring, alg, *inputDesc[0]->as<MKLDNNMemoryDesc>(),
new mkldnn::lrn_forward::desc(mkldnn::prop_kind::forward_scoring, alg, MemoryDescUtils::convertToMKLDNNMemoryDesc(*inputDesc[0]),
size, alpha, beta, k)));
descs.push_back(desc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <mkldnn_types.h>
#include <mkldnn_extension_utils.h>
#include <utils/general_utils.h>
#include <cpu_memory_desc_utils.h>

using namespace mkldnn;
using namespace MKLDNNPlugin;
Expand Down Expand Up @@ -180,8 +181,8 @@ bool MKLDNNPoolingNode::created() const {

void MKLDNNPoolingNode::createDescriptor(const std::vector<const MemoryDesc*> &inputDesc,
const std::vector<const MemoryDesc*> &outputDesc) {
MKLDNNMemoryDesc in_candidate = *inputDesc[0]->as<MKLDNNMemoryDesc>();
MKLDNNMemoryDesc out_candidate = *outputDesc[0]->as<MKLDNNMemoryDesc>();
MKLDNNMemoryDesc in_candidate = MemoryDescUtils::convertToMKLDNNMemoryDesc(*inputDesc[0]);
MKLDNNMemoryDesc out_candidate = MemoryDescUtils::convertToMKLDNNMemoryDesc(*outputDesc[0]);

mkldnn::algorithm alg;
if (algorithm == PoolingAvg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string>
#include <mkldnn_types.h>
#include <mkldnn_extension_utils.h>
#include <cpu_memory_desc_utils.h>

using namespace mkldnn;
using namespace MKLDNNPlugin;
Expand Down Expand Up @@ -119,7 +120,7 @@ bool MKLDNNSoftMaxNode::created() const {

void MKLDNNSoftMaxNode::createDescriptor(const std::vector<const MemoryDesc*> &inputDesc,
const std::vector<const MemoryDesc*> &outputDesc) {
MKLDNNMemoryDesc in_candidate = *inputDesc[0]->as<MKLDNNMemoryDesc>();
MKLDNNMemoryDesc in_candidate = MemoryDescUtils::convertToMKLDNNMemoryDesc(*inputDesc[0]);

MKLDNNDescriptor desc(std::shared_ptr<softmax_forward::desc>(
new softmax_forward::desc(prop_kind::forward_scoring, in_candidate, axis)));
Expand Down
Loading

0 comments on commit f38c336

Please sign in to comment.