Skip to content

Commit

Permalink
[cpu] Remove custom shape inference factories (openvinotoolkit#27924)
Browse files Browse the repository at this point in the history
### Details:
 - Remove custom shape inference factories CPU nodes.

### Related PR
- openvinotoolkit#27770 

### Tickets:
 -  CVS-118704

---------

Signed-off-by: Raasz, Pawel <[email protected]>
Co-authored-by: Michal Lukaszewski <[email protected]>
Co-authored-by: Maksim Kutakov <[email protected]>
  • Loading branch information
3 people authored and MirceaDan99 committed Jan 22, 2025
1 parent f1dd30e commit 87235ee
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 76 deletions.
11 changes: 5 additions & 6 deletions src/frontends/tensorflow/src/frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,12 +466,11 @@ std::shared_ptr<ov::Model> FrontEnd::convert(const ov::frontend::InputModel::Ptr

// recommend to use openvino-tokenizers if some unconverted operations from tokenizers are met
if (unsupported_ops_from_tokenizers.size() > 0) {
exception_message
<< "\nEncountered unconverted operation(s) for which openvino-tokenizers package "
"provides conversion extension(s): "
<< unsupported_ops_from_tokenizers
<< ". Install OpenVINO Tokenizers, refer to the documentation: "
"https://docs.openvino.ai/2024/openvino-workflow-generative/ov-tokenizers.html \n";
exception_message << "\nEncountered unconverted operation(s) for which openvino-tokenizers package "
"provides conversion extension(s): "
<< unsupported_ops_from_tokenizers
<< ". Install OpenVINO Tokenizers, refer to the documentation: "
"https://docs.openvino.ai/2024/openvino-workflow-generative/ov-tokenizers.html \n";
}
}

Expand Down
37 changes: 32 additions & 5 deletions src/plugins/intel_cpu/src/nodes/deconv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,43 @@ bool DeconvKey::operator==(const DeconvKey& rhs) const {
* input. Since in case it exists, plugin should pass the input data to the shape inference function.
*
*/
class DeconfolutionShapeInferFactory : public ShapeInferFactory {
class DeconvolutionShapeInferFactory : public ShapeInferFactory {
public:
DeconfolutionShapeInferFactory(std::shared_ptr<ov::Node> op) : m_op(std::move(op)) {}
DeconvolutionShapeInferFactory(std::shared_ptr<ov::Node> op) : m_op(std::move(op)) {}

ShapeInferPtr makeShapeInfer() const override {
const auto port_mask = (m_op->get_input_size() > 2) ? PortMask(2) : EMPTY_PORT_MASK;
return make_shape_inference(m_op, port_mask);
return std::make_shared<DeconvolutionShapeInfer>(m_op);
}

private:
class DeconvolutionShapeInfer : public IShapeInfer {
public:
DeconvolutionShapeInfer(const std::shared_ptr<ov::Node>& op)
: m_shape_infer(make_shape_inference(op)),
m_port_mask((op->get_input_size() > 2) ? PortMask(2) : EMPTY_PORT_MASK) {}

Result infer(const std::vector<std::reference_wrapper<const VectorDims>>& input_shapes,
const std::unordered_map<size_t, MemoryPtr>& data_dependency) override {
return m_shape_infer->infer(input_shapes, data_dependency);
}

const ov::CoordinateDiff& get_pads_begin() override {
return m_shape_infer->get_pads_begin();
}

const ov::CoordinateDiff& get_pads_end() override {
return m_shape_infer->get_pads_end();
}

port_mask_t get_port_mask() const override {
return m_port_mask;
};

private:
ShapeInferPtr m_shape_infer;
const port_mask_t m_port_mask;
};

std::shared_ptr<ov::Node> m_op;
};
} // namespace
Expand Down Expand Up @@ -165,7 +192,7 @@ bool Deconvolution::isSupportedOperation(const std::shared_ptr<const ov::Node>&
}

Deconvolution::Deconvolution(const std::shared_ptr<ov::Node>& op, const GraphContext::CPtr& context)
: Node(op, context, DeconfolutionShapeInferFactory(op)) {
: Node(op, context, DeconvolutionShapeInferFactory(op)) {
std::string errorMessage;
if (!isSupportedOperation(op, errorMessage))
OPENVINO_THROW_NOT_IMPLEMENTED(errorMessage);
Expand Down
16 changes: 1 addition & 15 deletions src/plugins/intel_cpu/src/nodes/eye.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,8 @@ bool Eye::isSupportedOperation(const std::shared_ptr<const ov::Node>& op, std::s
return true;
}

namespace {
class EyeShapeInferFactory : public ShapeInferFactory {
public:
EyeShapeInferFactory(std::shared_ptr<ov::Node> op) : m_op(std::move(op)) {}
ShapeInferPtr makeShapeInfer() const override {
return (m_op->get_input_size() == 4) ? make_shape_inference(m_op)
: make_shape_inference(m_op, PortMask(Eye::ROWS_NUM, Eye::COLS_NUM));
}

private:
std::shared_ptr<ov::Node> m_op;
};
} // namespace

Eye::Eye(const std::shared_ptr<ov::Node>& op, const GraphContext::CPtr& context)
: Node(op, context, EyeShapeInferFactory(op)) {
: Node(op, context, NgraphShapeInferFactory(op)) {
std::string errorMessage;
if (!isSupportedOperation(op, errorMessage)) {
OPENVINO_THROW_NOT_IMPLEMENTED(errorMessage);
Expand Down
30 changes: 15 additions & 15 deletions src/plugins/intel_cpu/src/nodes/reference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,10 @@
namespace ov {
namespace intel_cpu {

class ReferenceShapeInferFactory : public ShapeInferFactory {
public:
ReferenceShapeInferFactory(std::shared_ptr<ov::Node> op) : m_op{std::move(op)} {}

ShapeInferPtr makeShapeInfer() const override {
return make_shape_inference(m_op, FULL_PORT_MASK);
}

private:
std::shared_ptr<ov::Node> m_op;
};

namespace node {

Reference::Reference(const std::shared_ptr<ov::Node>& op, const GraphContext::CPtr& context, std::string errorMessage)
: Node(op, context, ReferenceShapeInferFactory(op)),
: Node(op, context, NgraphShapeInferFactory(op)),
ovCoreNode(op),
additionalErrorMessage(std::move(errorMessage)) {
if (!op->has_evaluate()) {
Expand Down Expand Up @@ -61,7 +49,9 @@ void Reference::initSupportedPrimitiveDescriptors() {
addSupportedPrimDesc(inputConfigurators, outputConfigurators, impl_desc_type::ref);
}

void Reference::createPrimitive() {}
void Reference::createPrimitive() {
hasOutputShapeDataDependency = isDynamicNode() && outputShapeDataDependency();
}

void Reference::execute(const dnnl::stream& strm) {
auto inputs = prepareInputs();
Expand All @@ -72,6 +62,14 @@ void Reference::execute(const dnnl::stream& strm) {
}

void Reference::executeDynamicImpl(const dnnl::stream& strm) {
if (!hasOutputShapeDataDependency) {
// if there is no data dependency for the output shape, we can execute the operation as is, similar to the
// static case, since the shapes are already calculated
execute(strm);
return;
}

// if there is data dependency, we need to perform shape inference first
auto inputs = prepareInputs();
ov::TensorVector outputs;
auto result = Node::shapeInfer();
Expand Down Expand Up @@ -125,7 +123,9 @@ bool Reference::created() const {
}

bool Reference::needShapeInfer() const {
return false;
// If there is data dependency for the output shape, let's assume the node has internal dynamism (in general case),
// so we postpone the shape inference until the actual execution
return !hasOutputShapeDataDependency && Node::needShapeInfer();
}

ov::TensorVector Reference::prepareInputs() const {
Expand Down
1 change: 1 addition & 0 deletions src/plugins/intel_cpu/src/nodes/reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Reference : public Node {
private:
const std::shared_ptr<ov::Node> ovCoreNode;
const std::string additionalErrorMessage;
bool hasOutputShapeDataDependency = false; // flag to cache the output shape data dependency check result
};

} // namespace node
Expand Down
41 changes: 7 additions & 34 deletions src/plugins/intel_cpu/src/shape_inference/shape_inference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,7 @@ class ShapeInferFallback : public ShapeInferBase {

ov::optional<std::vector<StaticShape>> infer(const std::vector<StaticShapeRef>& input_shapes,
const ov::ITensorAccessor& tensor_accessor) override {
auto op = m_node.get();
std::vector<StaticShape> output_shapes;
const auto op = m_node.get();

std::shared_ptr<ov::Node> local_op;
ov::OutputVector new_inputs;
Expand All @@ -252,7 +251,7 @@ class ShapeInferFallback : public ShapeInferBase {
local_op = op->clone_with_new_inputs(new_inputs);
local_op->validate_and_infer_types();

output_shapes.resize(local_op->get_output_size());
std::vector<StaticShape> output_shapes(local_op->get_output_size());
for (size_t i = 0; i < output_shapes.size(); ++i) {
const auto& partial_shape = local_op->get_output_partial_shape(i);

Expand All @@ -265,6 +264,11 @@ class ShapeInferFallback : public ShapeInferBase {

return {std::move(output_shapes)};
}

port_mask_t get_port_mask() const override {
// For fallback return full port mask to try get data for all node's inputs
return FULL_PORT_MASK;
}
};

template <class TOp, IShapeInfer::port_mask_t MASK>
Expand Down Expand Up @@ -610,34 +614,6 @@ const IStaticShapeInferFactory::TRegistry IStaticShapeInferFactory::registry{
#undef _OV_OP_SHAPE_INFER_MASK_REG
#undef _OV_OP_SHAPE_INFER_VA_REG

class ShapeInferCustomMask : public IShapeInfer {
public:
ShapeInferCustomMask(ShapeInferPtr shape_infer, port_mask_t port_mask)
: m_shape_infer{std::move(shape_infer)},
m_port_mask{port_mask} {}

Result infer(const std::vector<std::reference_wrapper<const VectorDims>>& input_shapes,
const std::unordered_map<size_t, MemoryPtr>& data_dependency) override {
return m_shape_infer->infer(input_shapes, data_dependency);
}

const ov::CoordinateDiff& get_pads_begin() override {
return m_shape_infer->get_pads_begin();
}

const ov::CoordinateDiff& get_pads_end() override {
return m_shape_infer->get_pads_end();
}

port_mask_t get_port_mask() const override {
return m_port_mask;
}

private:
const ShapeInferPtr m_shape_infer;
const port_mask_t m_port_mask;
};

std::shared_ptr<IStaticShapeInfer> make_shape_inference(std::shared_ptr<ov::Node> op) {
if (auto shape_infer = IStaticShapeInferFactory::make(op->get_type_info(), op)) {
return shape_infer;
Expand All @@ -652,8 +628,5 @@ std::shared_ptr<IStaticShapeInfer> make_shape_inference(std::shared_ptr<ov::Node
}
}

ShapeInferPtr make_shape_inference(std::shared_ptr<ov::Node> op, IShapeInfer::port_mask_t port_mask) {
return std::make_shared<ShapeInferCustomMask>(make_shape_inference(std::move(op)), port_mask);
}
} // namespace intel_cpu
} // namespace ov
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,5 @@ class IStaticShapeInfer : public IShapeInfer {
};

std::shared_ptr<IStaticShapeInfer> make_shape_inference(std::shared_ptr<ov::Node> op);
ShapeInferPtr make_shape_inference(std::shared_ptr<ov::Node> op, IShapeInfer::port_mask_t port_mask);
} // namespace intel_cpu
} // namespace ov

0 comments on commit 87235ee

Please sign in to comment.