Skip to content

Commit

Permalink
[OpenVINO] Add HW patterns (#2822)
Browse files Browse the repository at this point in the history
### Changes

Add HW patterns covering mask_rcnn_resnet50_atrous_coco model.
From an execution graph perspective, the subgraph of a model with a new
quantization scheme executes up to 3x faster.
Before | After

![image](https://github.com/user-attachments/assets/c94df9db-9b05-4ffa-9a30-4c93cc5127f1)


### Reason for changes
Improve performance. 

Local measurements:
Before: 1.8666 FPS | After: 2.0266 FPS 
Speed up ~ 8-9%
### Related tickets

144218

### Tests

Manually tested on the model.
  • Loading branch information
kshpv authored Jul 18, 2024
1 parent 380d2bb commit 2a83751
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions nncf/common/graph/patterns/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ class HWFusedPatternNames(Enum):
LINEAR_ARITHMETIC_ACTIVATIONS_ARITHMETIC = PatternDesc("linear_arithmetic_activations_arithmetic")
LINEAR_BATCH_NORM = PatternDesc("linear_batch_norm")
LINEAR_BATCH_NORM_ACTIVATIONS = PatternDesc("linear_batch_norm_activations")
# MaskRCNN_Resnet_Atrous
LINEAR_BATCH_TO_SPACE_SCALE_SHIFT_ACTIVATIONS = PatternDesc("linear_batch_to_space_scale_shift_activations")
LINEAR_BATCH_TO_SPACE_ARITHMETIC_ACTIVATIONS = PatternDesc("linear_batch_to_space_arithmetic_activations")
LINEAR_BATCH_NORM_SCALE_SHIFT_ACTIVATIONS = PatternDesc("linear_batch_norm_scale_shift_activations")
LINEAR_SCALE_SHIFT_ACTIVATIONS = PatternDesc("linear_scale_shift_activations")
LINEAR_CONST_MULTIPLY = PatternDesc("linear_const_multiply")
Expand Down
34 changes: 34 additions & 0 deletions nncf/openvino/hardware/fused_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,32 @@ def create_linear_biased_activation_scale_shift() -> GraphPattern:
return linear_biased


@OPENVINO_HW_FUSED_PATTERNS.register(HWFusedPatternNames.LINEAR_BATCH_TO_SPACE_SCALE_SHIFT_ACTIVATIONS)
def create_linear_batch_to_space_scale_shift_activations() -> GraphPattern:
linear = linear_operations()
batch_to_space = batch_to_space_operation()
scale_shift = create_scale_shift()
activations = atomic_activations_operations()

linear.join_patterns(batch_to_space)
linear.join_patterns(scale_shift)
linear.join_patterns(activations)
return linear


@OPENVINO_HW_FUSED_PATTERNS.register(HWFusedPatternNames.LINEAR_BATCH_TO_SPACE_ARITHMETIC_ACTIVATIONS)
def create_linear_batch_to_space_arithmetic_activations() -> GraphPattern:
linear = linear_operations()
batch_to_space = batch_to_space_operation()
arithmetic = arithmetic_operations()
activations = atomic_activations_operations()

linear.join_patterns(batch_to_space)
linear.join_patterns(arithmetic)
linear.join_patterns(activations)
return linear


@OPENVINO_HW_FUSED_PATTERNS.register(HWFusedPatternNames.LINEAR_ELEMENTWISE)
def create_linear_elementwise() -> GraphPattern:
linear = linear_operations()
Expand Down Expand Up @@ -748,6 +774,14 @@ def unsqueeze_operation() -> GraphPattern:
return pattern


def batch_to_space_operation() -> GraphPattern:
pattern = GraphPattern()
pattern.add_node(
**{GraphPattern.LABEL_ATTR: "BATCH_TO_SPACE", GraphPattern.METATYPE_ATTR: om.OVBatchToSpaceMetatype}
)
return pattern


def create_input_convert_transpose() -> GraphPattern:
pattern = GraphPattern()
model_input = pattern.add_node(
Expand Down
2 changes: 2 additions & 0 deletions tests/onnx/test_pattern_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
HWFusedPatternNames.MVN_SCALE_SHIFT_ACTIVATIONS: "Not relevant for ONNX.",
HWFusedPatternNames.LINEAR_ACTIVATIONS_UNSQUEEZE_BN_SQUEEZE: "Not relevant for ONNX.",
HWFusedPatternNames.ARITHMETIC_ACTIVATIONS_ARITHMETIC: "Not relevant for ONNX.",
HWFusedPatternNames.LINEAR_BATCH_TO_SPACE_ARITHMETIC_ACTIVATIONS: "Not relevant for ONNX.",
HWFusedPatternNames.LINEAR_BATCH_TO_SPACE_SCALE_SHIFT_ACTIVATIONS: "Not relevant for ONNX.",
}

IGNORING_IGNORED_PATTERN_REASONS = {
Expand Down
2 changes: 2 additions & 0 deletions tests/torch/test_pattern_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
HWFusedPatternNames.MVN: "Not relevant for Torch.",
HWFusedPatternNames.GELU: "Not relevant for Torch.",
HWFusedPatternNames.ARITHMETIC_ACTIVATIONS_ARITHMETIC: "Not relevant for Torch.",
HWFusedPatternNames.LINEAR_BATCH_TO_SPACE_ARITHMETIC_ACTIVATIONS: "Not relevant for Torch.",
HWFusedPatternNames.LINEAR_BATCH_TO_SPACE_SCALE_SHIFT_ACTIVATIONS: "Not relevant for Torch.",
}

IGNORING_IGNORED_PATTERN_REASONS = {
Expand Down

0 comments on commit 2a83751

Please sign in to comment.