Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cpu] Remove custom shape inference factories #27924

Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
8fa1fdd
Remove custom shape inference factories
praasz Dec 4, 2024
1499658
Update FakeQunatize shape inference to return dedicated port mask
praasz Dec 5, 2024
da7e5a8
Fallback shape inference use full port mask
praasz Dec 5, 2024
3829b7b
Add custom shape inference for deconv node
praasz Dec 6, 2024
e40addb
Add custom mask for MaxPool for CPU data dependency only
praasz Dec 9, 2024
75dfef5
Remove commented code
praasz Dec 9, 2024
240a3d3
Merge branch 'master' into shape-infer/remove-cpu-custom-shape-infer-…
praasz Dec 11, 2024
683efa9
Merge remote-tracking branch 'origin/master' into shape-infer/remove-…
praasz Dec 16, 2024
2d29210
Merge branch 'master' into shape-infer/remove-cpu-custom-shape-infer-…
praasz Dec 20, 2024
f5fbe63
Merge branch 'master' into shape-infer/remove-cpu-custom-shape-infer-…
praasz Jan 7, 2025
df86e25
Merge branch 'master' into shape-infer/remove-cpu-custom-shape-infer-…
mlukasze Jan 8, 2025
e21a7eb
Fix condition for skipping shape inference in Ref node and remove unn…
maxnick Jan 13, 2025
5e9de2a
Optimize shape inference for Reference node if shape hasn't changed
maxnick Jan 14, 2025
0a57ce7
Merge remote-tracking branch 'origin/master' into pr-27924
maxnick Jan 14, 2025
d268a96
Fix code style
maxnick Jan 14, 2025
7228927
Merge remote-tracking branch 'origin/master' into pr-27924
maxnick Jan 14, 2025
95c02ac
Merge branch 'master' into shape-infer/remove-cpu-custom-shape-infer-…
mlukasze Jan 17, 2025
b7a89aa
Merge branch 'master' into shape-infer/remove-cpu-custom-shape-infer-…
praasz Jan 17, 2025
646e42b
Merge remote-tracking branch 'origin/master' into shape-infer/remove-…
praasz Jan 17, 2025
a592978
Merge branch 'master' into shape-infer/remove-cpu-custom-shape-infer-…
mlukasze Jan 20, 2025
2993e77
Fix issue after merge
praasz Jan 20, 2025
3ac4d01
Remove move on const ref object
praasz Jan 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/frontends/tensorflow/src/frontend.cpp
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part was changed by clang-format.

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 @@ -31,22 +31,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(std::move(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
Loading