@pattern

Applicable Products

All processors

Function Usage

Defines a pattern using a Python expression. One @pattern method corresponds to one pattern. Multiple @pattern methods can be declared for multiple patterns.

Prototype

1
2
3
@pattern
def add_zero(self, inputs):
    return inputs[0] + 0

Parameters

Parameter

Input/Output

Description

inputs

Input

Input set of the expression pattern. inputs[i] indicates the ith graph input. inputs[:N] explicitly declares multiple consecutive graph inputs.

Returns

Type

Description

TensorHolder

Returns a single-output pattern expression.

list[TensorHolder] / tuple[TensorHolder, ...]

Returns a multi-output pattern. This list or tuple does not represent multiple patterns.

Restrictions

  • The @pattern method only declares a pattern and does not receive match result.
  • A multi-pattern pass is declared using multiple @pattern methods, and multiple patterns or graphs are not returned using a single method.
  • The number of inputs is unknown, so direct iteration is not possible. In multi-input scenarios, use inputs[:N].
  • The Python layer automatically creates the GraphBuilder, graph input, and graph output, and automatically captures the accessed inputs and the pattern output returned by @pattern.
  • The automatic capture sequence is fixed as follows: Capture the accessed inputs based on the input sequence number, and then capture the pattern output based on the return structure sequence. When a tensor is used as both an input and an output, it is captured once for each role.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from ge.passes import PatternFusionPass, pattern


class AlgebraicPass(PatternFusionPass):
    @pattern
    def add_zero(self, inputs):
        return inputs[0] + 0

    @pattern
    def mul_one(self, inputs):
        return inputs[0] * 1

    def replacement(self, inputs):
        return inputs[0]

For more usage details, see Example of a Fusion Pass.