Skip to content

Commit

Permalink
[LPT] Security fixes (openvinotoolkit#10243)
Browse files Browse the repository at this point in the history
  • Loading branch information
v-Golubev authored Feb 9, 2022
1 parent aedd902 commit d28f8b7
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ class LP_TRANSFORMATIONS_API FakeQuantizeDequantization {

static bool checkElementwise(const std::shared_ptr<ngraph::Node>& elementwise);

static bool checkShape(const std::shared_ptr<ngraph::Node>& elementwise) noexcept;
static bool checkShape(const std::shared_ptr<ngraph::Node>& elementwise);

static int fillDequantizationParams(
const std::shared_ptr<ngraph::Node>& elementwise,
std::shared_ptr<ngraph::opset1::Convert>& convert,
std::shared_ptr<ngraph::opset1::Constant>& constant) noexcept;
std::shared_ptr<ngraph::opset1::Constant>& constant);

static int fillDequantizationParams(
const std::shared_ptr<ngraph::Node>& elementwise,
std::shared_ptr<ngraph::opset1::Constant>& constant) noexcept;
std::shared_ptr<ngraph::opset1::Constant>& constant);

Output<Node> data;
std::shared_ptr<opset1::Convert> convert;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class LP_TRANSFORMATIONS_API ConcatTransformation : public LayerTransformation {
bool transform(TransformationContext& context, ngraph::pattern::Matcher &m) override;
bool isPrecisionPreserved(std::shared_ptr<Node> layer) const noexcept override;
bool canBeTransformed(const TransformationContext& context, std::shared_ptr<Node> layer) const override;
static bool isQuantizedStatic(const std::shared_ptr<const Node>& layer) noexcept;
static bool isQuantizedStatic(const std::shared_ptr<const Node>& layer);

protected:
static bool isHandled(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class LP_TRANSFORMATIONS_API EltwiseBaseTransformation : public LayerTransformat
bool canBeTransformed(const TransformationContext& context, std::shared_ptr<Node> layer) const override;
bool isPrecisionPreserved(std::shared_ptr<Node> layer) const noexcept override;

static bool isBroadcasted(const PartialShape& shape) noexcept;
static bool isBroadcasted(const PartialShape& shape);
protected:
int getNotEmpty(const std::shared_ptr<Node>& eltwise) const;
// Return indexes:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class LP_TRANSFORMATIONS_API MultiplyToGroupConvolutionTransformation : public L
bool transform(TransformationContext& context, ngraph::pattern::Matcher &m) override;
bool canBeTransformed(const TransformationContext& context, std::shared_ptr<Node> layer) const override;
bool isPrecisionPreserved(std::shared_ptr<Node> layer) const noexcept override;
bool isQuantized(const std::shared_ptr<const Node>& layer) const noexcept override;
static bool canBeTransformedToGroupConvolution(const std::shared_ptr<const Node>& layer) noexcept;
bool isQuantized(const std::shared_ptr<const Node>& layer) const override;
static bool canBeTransformedToGroupConvolution(const std::shared_ptr<const Node>& layer);
static bool isDynamicOrScalar(const std::shared_ptr<const Node>& node);

void setGroupSize(const size_t groupSize);
Expand Down
6 changes: 3 additions & 3 deletions src/common/low_precision_transformations/src/concat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ bool ConcatTransformation::isHandled(const TransformationContext& context, const
return false;
}

bool ConcatTransformation::isQuantizedStatic(const std::shared_ptr<const Node>& layer) noexcept {
bool ConcatTransformation::isQuantizedStatic(const std::shared_ptr<const Node>& layer) {
const auto concat = as_type_ptr<const opset1::Concat>(layer);
if (concat == nullptr) {
return false;
Expand All @@ -342,8 +342,8 @@ bool ConcatTransformation::isQuantizedStatic(const std::shared_ptr<const Node>&
return false;
}

const size_t normalizedAxis = ngraph::normalize_axis(concat->get_friendly_name(), concat->get_axis(), outputRank);
return normalizedAxis == 1ul;
const auto normalizedAxis = ngraph::normalize_axis(concat->get_friendly_name(), concat->get_axis(), outputRank);
return normalizedAxis == 1;
}

} // namespace low_precision
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ using namespace ngraph;
using namespace ngraph::pass;
using namespace ngraph::pass::low_precision;

bool EltwiseBaseTransformation::isBroadcasted(const PartialShape& shape) noexcept {
bool EltwiseBaseTransformation::isBroadcasted(const PartialShape& shape) {
const auto rank = shape.rank();
if (rank.is_dynamic()) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ bool FakeQuantizeDequantization::isLowPrecision() const {
return DataPrecision::isSupported(data.get_element_type());
}

bool FakeQuantizeDequantization::checkShape(const std::shared_ptr<ngraph::Node>& elementwise) noexcept {
bool FakeQuantizeDequantization::checkShape(const std::shared_ptr<ngraph::Node>& elementwise) {
std::shared_ptr<ngraph::opset1::Convert> convert;
std::shared_ptr<ngraph::opset1::Constant> constant;
const int branchIndex = FakeQuantizeDequantization::fillDequantizationParams(elementwise, convert, constant);
Expand All @@ -90,7 +90,7 @@ bool FakeQuantizeDequantization::checkShape(const std::shared_ptr<ngraph::Node>&
}

if (!inPShape.rank().is_dynamic()) {
for (int i = 0; i < inPShape.rank().get_length(); ++i) {
for (int i = 0; i < inPShape.size(); ++i) {
if (inPShape[i] != outPShape[i] && !inPShape[i].is_dynamic()) {
return false;
}
Expand Down Expand Up @@ -178,7 +178,7 @@ std::shared_ptr<Node> FakeQuantizeDequantization::copyWithNewInput(const std::sh
int FakeQuantizeDequantization::fillDequantizationParams(
const std::shared_ptr<ngraph::Node>& elementwise,
std::shared_ptr<ngraph::opset1::Convert>& convert,
std::shared_ptr<ngraph::opset1::Constant>& constant) noexcept {
std::shared_ptr<ngraph::opset1::Constant>& constant) {
auto fill = [](
const std::shared_ptr<ngraph::Node>& elementwise,
const size_t branchIndex,
Expand Down Expand Up @@ -211,7 +211,7 @@ int FakeQuantizeDequantization::fillDequantizationParams(

int FakeQuantizeDequantization::fillDequantizationParams(
const std::shared_ptr<ngraph::Node>& elementwise,
std::shared_ptr<ngraph::opset1::Constant>& constant) noexcept {
std::shared_ptr<ngraph::opset1::Constant>& constant) {
constant = elementwise->get_input_element_type(1ul).is_real() ?
ov::as_type_ptr<opset1::Constant>(elementwise->get_input_node_shared_ptr(1ul)) :
nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,11 @@ bool MultiplyToGroupConvolutionTransformation::canBeTransformed(const Transforma
return true;
}

bool MultiplyToGroupConvolutionTransformation::isQuantized(const std::shared_ptr<const Node>& layer) const noexcept {
bool MultiplyToGroupConvolutionTransformation::isQuantized(const std::shared_ptr<const Node>& layer) const {
return MultiplyToGroupConvolutionTransformation::canBeTransformedToGroupConvolution(layer);
}

bool MultiplyToGroupConvolutionTransformation::canBeTransformedToGroupConvolution(const std::shared_ptr<const Node>& layer) noexcept {
bool MultiplyToGroupConvolutionTransformation::canBeTransformedToGroupConvolution(const std::shared_ptr<const Node>& layer) {
const auto parent0 = layer->get_input_node_shared_ptr(0);
const auto parent1 = layer->get_input_node_shared_ptr(1);

Expand All @@ -219,7 +219,7 @@ bool MultiplyToGroupConvolutionTransformation::canBeTransformedToGroupConvolutio
return false;
}

return (pShape.rank().get_length() == 4ul) || (pShape.rank().get_length() == 5ul);
return (pShape.size() == 4ul) || (pShape.size() == 5ul);
}

bool MultiplyToGroupConvolutionTransformation::isDynamicOrScalar(const std::shared_ptr<const Node>& node) {
Expand Down
14 changes: 14 additions & 0 deletions src/common/low_precision_transformations/src/shuffle_channels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ bool ShuffleChannelsTransformation::canBeTransformed(const TransformationContext
return false;
}

// It's impossible to normalize a negative axis in case of dynamic rank
// but it's necessary when dequantization operations are per channel
if (shuffleChannels->get_input_partial_shape(0).rank().is_dynamic() && shuffleChannels->get_axis() < 0) {
const bool perChannelSub = dequantization.subtractConstant ?
ov::shape_size(dequantization.subtractConstant->get_shape()) > 0 :
false;
const bool perChannelMul = dequantization.multiplyConstant ?
ov::shape_size(dequantization.multiplyConstant->get_shape()) > 0 :
false;
if (perChannelMul || perChannelSub) {
return false;
}
}

return true;
}

Expand Down

0 comments on commit d28f8b7

Please sign in to comment.