Module furiosa.quantizer.frontend.onnx.transformer.eliminate_redundant_reshape_pattern

Expand source code
import onnx
import abc

from furiosa_sdk_quantizer.frontend.onnx.transformer import ONNXTransformer
from furiosa_sdk_quantizer.interfaces.transformer import Transformer


class EliminateRedundantReshapePattern(Transformer):
    def transform(self, model: onnx.ModelProto) -> onnx.ModelProto:
        for transformer in [
            Pattern_1,
            Pattern_2,
            Pattern_4,
            Pattern_5,
            Pattern_6,
            Pattern_7,
            Pattern_3,
        ]:
            model = transformer(model).transform()

        return model


class Pattern_1(ONNXTransformer, abc.ABC):
    """
        transform
            prev --> Flatten/Squeeze --> Unsqueeze --> next
        to
            prev --> (   ) --> next
        if prev.output[0].shape == next.input[0].shape
    """
    pattern_to_match = ['Flatten/Squeeze', 'Unsqueeze']

    def pattern_matching(self, base_node):
        inputs = base_node.input

        matched_nodes = self.pattern_matcher(base_node, self.pattern_to_match)
        if not matched_nodes:
            return inputs

        if not self.pattern_condition_checker(matched_nodes):
            return inputs

        top_node = matched_nodes[0]

        self.transform_to_eliminate(matched_nodes, top_node.input[0])
        return top_node.input

    def pattern_condition_checker(self, nodes_to_check):
        top_node = nodes_to_check[0]
        base_node = nodes_to_check[-1]

        if self.is_same_shape(top_node.input[0], base_node.output[0]):
            return True
        return False


class Pattern_2(Pattern_1, abc.ABC):
    """
        transform
            prev --> Reshape --> Flatten/Squeeze --> Unsqueeze --> next
        to
            prev --> (   ) --> next
        if prev.output[0].shape == next.input[0].shape
    """
    pattern_to_match = ['Reshape', 'Flatten/Squeeze', 'Unsqueeze']


class Pattern_3(Pattern_1, abc.ABC):
    """
        transform
            prev --> Reshape --> next
        to
            prev --> (   ) --> next
        if prev.output[0].shape == next.input[0].shape
    """
    pattern_to_match = ['Reshape']


class Pattern_4(Pattern_1, abc.ABC):
    """
        transform
            prev --> Reshape --> Expand --> Expand --> Reshape --> next

        to
            prev --> (   ) --> next
        if prev.output[0].shape == next.input[0].shape
    """
    pattern_to_match = ['Reshape', 'Expand', 'Expand', 'Reshape']


class Pattern_5(Pattern_1, abc.ABC):
    """
        transform
            prev --> Reshape --> Expand --> Reshape --> next
        to
            prev --> (   ) --> next
        if prev.output[0].shape == next.input[0].shape
    """
    pattern_to_match = ['Reshape', 'Expand', 'Reshape']


class Pattern_6(Pattern_1, abc.ABC):
    """
        transform
            prev --> Reshape --> Reshape --> next
        to
            prev --> (   ) --> next
        if prev.output[0].shape == next.input[0].shape
    """
    pattern_to_match = ['Reshape', 'Reshape']


class Pattern_7(Pattern_1, abc.ABC):
    """
        transform
            prev --> Reshape --> Reshape --> Reshape --> next
        to
            prev --> (   ) --> next
        if prev.output[0].shape == next.input[0].shape
    """
    pattern_to_match = ['Reshape', 'Reshape', 'Reshape']

Classes

class EliminateRedundantReshapePattern (*args, **kwds)

Abstract base class for generic types.

A generic type is typically declared by inheriting from this class parameterized with one or more type variables. For example, a generic mapping type might be defined as::

class Mapping(Generic[KT, VT]): def getitem(self, key: KT) -> VT: … # Etc.

This class can then be used as follows::

def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT: try: return mapping[key] except KeyError: return default

Expand source code
class EliminateRedundantReshapePattern(Transformer):
    def transform(self, model: onnx.ModelProto) -> onnx.ModelProto:
        for transformer in [
            Pattern_1,
            Pattern_2,
            Pattern_4,
            Pattern_5,
            Pattern_6,
            Pattern_7,
            Pattern_3,
        ]:
            model = transformer(model).transform()

        return model

Ancestors

  • furiosa_sdk_quantizer.interfaces.transformer.Transformer
  • typing.Generic

Methods

def transform(self, model: onnx.onnx_ml_pb2.ModelProto) ‑> onnx.onnx_ml_pb2.ModelProto
Expand source code
def transform(self, model: onnx.ModelProto) -> onnx.ModelProto:
    for transformer in [
        Pattern_1,
        Pattern_2,
        Pattern_4,
        Pattern_5,
        Pattern_6,
        Pattern_7,
        Pattern_3,
    ]:
        model = transformer(model).transform()

    return model
class Pattern_1 (model)

transform prev –> Flatten/Squeeze –> Unsqueeze –> next to prev –> ( ) –> next if prev.output[0].shape == next.input[0].shape

Expand source code
class Pattern_1(ONNXTransformer, abc.ABC):
    """
        transform
            prev --> Flatten/Squeeze --> Unsqueeze --> next
        to
            prev --> (   ) --> next
        if prev.output[0].shape == next.input[0].shape
    """
    pattern_to_match = ['Flatten/Squeeze', 'Unsqueeze']

    def pattern_matching(self, base_node):
        inputs = base_node.input

        matched_nodes = self.pattern_matcher(base_node, self.pattern_to_match)
        if not matched_nodes:
            return inputs

        if not self.pattern_condition_checker(matched_nodes):
            return inputs

        top_node = matched_nodes[0]

        self.transform_to_eliminate(matched_nodes, top_node.input[0])
        return top_node.input

    def pattern_condition_checker(self, nodes_to_check):
        top_node = nodes_to_check[0]
        base_node = nodes_to_check[-1]

        if self.is_same_shape(top_node.input[0], base_node.output[0]):
            return True
        return False

Ancestors

  • furiosa_sdk_quantizer.frontend.onnx.transformer.ONNXTransformer
  • abc.ABC

Subclasses

Class variables

var pattern_to_match

Methods

def pattern_condition_checker(self, nodes_to_check)
Expand source code
def pattern_condition_checker(self, nodes_to_check):
    top_node = nodes_to_check[0]
    base_node = nodes_to_check[-1]

    if self.is_same_shape(top_node.input[0], base_node.output[0]):
        return True
    return False
def pattern_matching(self, base_node)
Expand source code
def pattern_matching(self, base_node):
    inputs = base_node.input

    matched_nodes = self.pattern_matcher(base_node, self.pattern_to_match)
    if not matched_nodes:
        return inputs

    if not self.pattern_condition_checker(matched_nodes):
        return inputs

    top_node = matched_nodes[0]

    self.transform_to_eliminate(matched_nodes, top_node.input[0])
    return top_node.input
class Pattern_2 (model)

transform prev –> Reshape –> Flatten/Squeeze –> Unsqueeze –> next to prev –> ( ) –> next if prev.output[0].shape == next.input[0].shape

Expand source code
class Pattern_2(Pattern_1, abc.ABC):
    """
        transform
            prev --> Reshape --> Flatten/Squeeze --> Unsqueeze --> next
        to
            prev --> (   ) --> next
        if prev.output[0].shape == next.input[0].shape
    """
    pattern_to_match = ['Reshape', 'Flatten/Squeeze', 'Unsqueeze']

Ancestors

  • Pattern_1
  • furiosa_sdk_quantizer.frontend.onnx.transformer.ONNXTransformer
  • abc.ABC

Class variables

var pattern_to_match
class Pattern_3 (model)

transform prev –> Reshape –> next to prev –> ( ) –> next if prev.output[0].shape == next.input[0].shape

Expand source code
class Pattern_3(Pattern_1, abc.ABC):
    """
        transform
            prev --> Reshape --> next
        to
            prev --> (   ) --> next
        if prev.output[0].shape == next.input[0].shape
    """
    pattern_to_match = ['Reshape']

Ancestors

  • Pattern_1
  • furiosa_sdk_quantizer.frontend.onnx.transformer.ONNXTransformer
  • abc.ABC

Class variables

var pattern_to_match
class Pattern_4 (model)

transform prev –> Reshape –> Expand –> Expand –> Reshape –> next

to prev –> ( ) –> next if prev.output[0].shape == next.input[0].shape

Expand source code
class Pattern_4(Pattern_1, abc.ABC):
    """
        transform
            prev --> Reshape --> Expand --> Expand --> Reshape --> next

        to
            prev --> (   ) --> next
        if prev.output[0].shape == next.input[0].shape
    """
    pattern_to_match = ['Reshape', 'Expand', 'Expand', 'Reshape']

Ancestors

  • Pattern_1
  • furiosa_sdk_quantizer.frontend.onnx.transformer.ONNXTransformer
  • abc.ABC

Class variables

var pattern_to_match
class Pattern_5 (model)

transform prev –> Reshape –> Expand –> Reshape –> next to prev –> ( ) –> next if prev.output[0].shape == next.input[0].shape

Expand source code
class Pattern_5(Pattern_1, abc.ABC):
    """
        transform
            prev --> Reshape --> Expand --> Reshape --> next
        to
            prev --> (   ) --> next
        if prev.output[0].shape == next.input[0].shape
    """
    pattern_to_match = ['Reshape', 'Expand', 'Reshape']

Ancestors

  • Pattern_1
  • furiosa_sdk_quantizer.frontend.onnx.transformer.ONNXTransformer
  • abc.ABC

Class variables

var pattern_to_match
class Pattern_6 (model)

transform prev –> Reshape –> Reshape –> next to prev –> ( ) –> next if prev.output[0].shape == next.input[0].shape

Expand source code
class Pattern_6(Pattern_1, abc.ABC):
    """
        transform
            prev --> Reshape --> Reshape --> next
        to
            prev --> (   ) --> next
        if prev.output[0].shape == next.input[0].shape
    """
    pattern_to_match = ['Reshape', 'Reshape']

Ancestors

  • Pattern_1
  • furiosa_sdk_quantizer.frontend.onnx.transformer.ONNXTransformer
  • abc.ABC

Class variables

var pattern_to_match
class Pattern_7 (model)

transform prev –> Reshape –> Reshape –> Reshape –> next to prev –> ( ) –> next if prev.output[0].shape == next.input[0].shape

Expand source code
class Pattern_7(Pattern_1, abc.ABC):
    """
        transform
            prev --> Reshape --> Reshape --> Reshape --> next
        to
            prev --> (   ) --> next
        if prev.output[0].shape == next.input[0].shape
    """
    pattern_to_match = ['Reshape', 'Reshape', 'Reshape']

Ancestors

  • Pattern_1
  • furiosa_sdk_quantizer.frontend.onnx.transformer.ONNXTransformer
  • abc.ABC

Class variables

var pattern_to_match