furiosa.quantizer.frontend.onnx.transformer package

Subpackages

Submodules

furiosa.quantizer.frontend.onnx.transformer.convert_2d_sum_to_add module

class furiosa.quantizer.frontend.onnx.transformer.convert_2d_sum_to_add.Convert2dSumToAdd(*args, **kwds)

Bases: furiosa.quantizer.interfaces.transformer.Transformer

transform(model: onnx.onnx_ml_pb2.ModelProto) onnx.onnx_ml_pb2.ModelProto

furiosa.quantizer.frontend.onnx.transformer.convert_conv1d_to_conv2d module

class furiosa.quantizer.frontend.onnx.transformer.convert_conv1d_to_conv2d.ConvertConv1dToConv2d(*args, **kwds)

Bases: furiosa.quantizer.interfaces.transformer.Transformer

transform(model: onnx.onnx_ml_pb2.ModelProto) onnx.onnx_ml_pb2.ModelProto
class furiosa.quantizer.frontend.onnx.transformer.convert_conv1d_to_conv2d.Pattern_1(model)

Bases: furiosa.quantizer.frontend.onnx.transformer.ONNXTransformer

transform

prev –> Reshape –> Conv –> Reshape –> next

to

prev –> Reshape –> Conv –> Reshape –> next

if Conv.input[0].ndim == 3, i.e., if Conv1d

get_attrs(mid_node)
pattern_condition_checker(nodes_to_check)
pattern_matching(base_node)
pattern_to_match = ['Reshape', 'Conv', 'Reshape']

furiosa.quantizer.frontend.onnx.transformer.eliminate_redundant_shape_pattern module

class furiosa.quantizer.frontend.onnx.transformer.eliminate_redundant_shape_pattern.EliminateRedundantShapePattern(*args, **kwds)

Bases: furiosa.quantizer.interfaces.transformer.Transformer

transform(model: onnx.onnx_ml_pb2.ModelProto) onnx.onnx_ml_pb2.ModelProto
class furiosa.quantizer.frontend.onnx.transformer.eliminate_redundant_shape_pattern.Pattern_1(model)

Bases: furiosa.quantizer.frontend.onnx.transformer.ONNXTransformer

transform

prev –> Flatten/Squeeze –> Unsqueeze –> next

to

prev –> ( ) –> next

if prev.output[0].shape == next.input[0].shape

pattern_condition_checker(nodes_to_check)
pattern_matching(base_node)
pattern_to_match = ['Flatten/Squeeze', 'Unsqueeze']
class furiosa.quantizer.frontend.onnx.transformer.eliminate_redundant_shape_pattern.Pattern_2(model)

Bases: furiosa.quantizer.frontend.onnx.transformer.eliminate_redundant_shape_pattern.Pattern_1

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 furiosa.quantizer.frontend.onnx.transformer.eliminate_redundant_shape_pattern.Pattern_3(model)

Bases: furiosa.quantizer.frontend.onnx.transformer.eliminate_redundant_shape_pattern.Pattern_1

transform

prev –> Reshape –> next

to

prev –> ( ) –> next

if prev.output[0].shape == next.input[0].shape

pattern_to_match = ['Reshape']
class furiosa.quantizer.frontend.onnx.transformer.eliminate_redundant_shape_pattern.Pattern_4(model)

Bases: furiosa.quantizer.frontend.onnx.transformer.eliminate_redundant_shape_pattern.Pattern_1

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 furiosa.quantizer.frontend.onnx.transformer.eliminate_redundant_shape_pattern.Pattern_5(model)

Bases: furiosa.quantizer.frontend.onnx.transformer.eliminate_redundant_shape_pattern.Pattern_1

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 furiosa.quantizer.frontend.onnx.transformer.eliminate_redundant_shape_pattern.Pattern_6(model)

Bases: furiosa.quantizer.frontend.onnx.transformer.eliminate_redundant_shape_pattern.Pattern_1

transform

prev –> Reshape –> Reshape –> next

to

prev –> ( ) –> next

if prev.output[0].shape == next.input[0].shape

pattern_to_match = ['Reshape', 'Reshape']
class furiosa.quantizer.frontend.onnx.transformer.eliminate_redundant_shape_pattern.Pattern_7(model)

Bases: furiosa.quantizer.frontend.onnx.transformer.eliminate_redundant_shape_pattern.Pattern_1

transform

prev –> Reshape –> Reshape –> Reshape –> next

to

prev –> ( ) –> next

if prev.output[0].shape == next.input[0].shape

pattern_to_match = ['Reshape', 'Reshape', 'Reshape']
class furiosa.quantizer.frontend.onnx.transformer.eliminate_redundant_shape_pattern.Pattern_8(model)

Bases: furiosa.quantizer.frontend.onnx.transformer.eliminate_redundant_shape_pattern.Pattern_1

transform

prev –> Expand –> next

to

prev –> ( ) –> next

if prev.output[0].shape == next.input[0].shape

pattern_to_match = ['Expand']

furiosa.quantizer.frontend.onnx.transformer.fuse_bn_into_conv module

class furiosa.quantizer.frontend.onnx.transformer.fuse_bn_into_conv.FuseBnIntoConv(*args, **kwds)

Bases: furiosa.quantizer.interfaces.transformer.Transformer

transform(model: onnx.onnx_ml_pb2.ModelProto) onnx.onnx_ml_pb2.ModelProto
class furiosa.quantizer.frontend.onnx.transformer.fuse_bn_into_conv.Pattern_1(model)

Bases: furiosa.quantizer.frontend.onnx.transformer.ONNXTransformer

transform

prev –> Conv –> BatchNormalization –> next

to

prev –> Conv –> next

static fuse_bn_params(weight, multiplier, shifter)
get_bn_params(node)
static get_multiplier_and_shifter(scale, B, mean, var, eps)
make_new_init(matched_nodes)
make_new_node(matched_nodes)
make_new_vi(matched_nodes)
pattern_matching(base_node)
pattern_to_match = ['Conv', 'BatchNormalization']
class furiosa.quantizer.frontend.onnx.transformer.fuse_bn_into_conv.Pattern_2(model)

Bases: furiosa.quantizer.frontend.onnx.transformer.fuse_bn_into_conv.Pattern_1

transform

prev –> BatchNormalization –> next

to

prev –> Mul –> Add –> next

if prev.op_type != Conv

make_new_init(matched_nodes)
make_new_node(matched_nodes)
make_new_vi(matched_nodes)
pattern_condition_checker(nodes_to_check)
pattern_to_match = ['BatchNormalization']
class furiosa.quantizer.frontend.onnx.transformer.fuse_bn_into_conv.Pattern_3(model)

Bases: furiosa.quantizer.frontend.onnx.transformer.fuse_bn_into_conv.Pattern_1

transform

prev –> Conv –> Mul –> Add –> next

to

prev –> Conv –> next

if 1. Mul has only one initializer
  1. Add has only one initializer

check_condition_1(node)
get_multiplier_and_shifter(mul_node, add_node)
make_new_init(matched_nodes)
make_new_node(matched_nodes)
pattern_condition_checker(nodes_to_check)
pattern_matching(base_node)
pattern_to_match = ['Conv', 'Mul', 'Add']

furiosa.quantizer.frontend.onnx.transformer.fuse_bn_into_convtranspose module

class furiosa.quantizer.frontend.onnx.transformer.fuse_bn_into_convtranspose.FuseBnIntoConvTranspose(*args, **kwds)

Bases: furiosa.quantizer.interfaces.transformer.Transformer

transform(model: onnx.onnx_ml_pb2.ModelProto) onnx.onnx_ml_pb2.ModelProto
class furiosa.quantizer.frontend.onnx.transformer.fuse_bn_into_convtranspose.Pattern_1(model)

Bases: furiosa.quantizer.frontend.onnx.transformer.fuse_bn_into_conv.Pattern_1

transform

prev –> ConvTranspose –> BatchNormalization –> next

to

prev –> ConvTranspose –> next

static fuse_bn_params(weight, multiplier, shifter)
make_new_node(matched_nodes)
pattern_to_match = ['ConvTranspose', 'BatchNormalization']

furiosa.quantizer.frontend.onnx.transformer.fuse_conv module

class furiosa.quantizer.frontend.onnx.transformer.fuse_conv.FuseConv(*args, **kwds)

Bases: furiosa.quantizer.interfaces.transformer.Transformer

transform(model: onnx.onnx_ml_pb2.ModelProto) onnx.onnx_ml_pb2.ModelProto
class furiosa.quantizer.frontend.onnx.transformer.fuse_conv.Pattern_1(model)

Bases: furiosa.quantizer.frontend.onnx.transformer.ONNXTransformer

transform

prev –> MatMul –> Add –> next

to

prev –> Unsqueeze –> Conv –> Squeeze –> next

if 1. MatMul.ndim == 2
  1. MatMul must have at most one initializer

  2. Add must have at most one initializer

check_condition_1(tensor_name)
check_condition_2(node)
get_new_init_args(matched_nodes)
get_new_node_args(matched_nodes)
get_new_vi_args(matched_nodes)
make_initializers(w_input, b_input=None, **kwargs)
make_nodes(node_input, node_output, w_input, b_input, **kwargs)
make_value_infos(node_input, node_output)
pattern_condition_checker(nodes_to_check)
pattern_matching(base_node)
pattern_to_match = ['MatMul', 'Add']
weight_transformation(w_arr, **kwargs)
class furiosa.quantizer.frontend.onnx.transformer.fuse_conv.Pattern_2(model)

Bases: furiosa.quantizer.frontend.onnx.transformer.fuse_conv.Pattern_1

transform

prev –> Gemm –> next

to

prev –> Unsqueeze –> Conv –> Squeeze –> next

if 1. one of Gemm.A and Gemm.B must have initializer
  1. Gemm.C must have initializer if defined

check_condition_3(node)
check_condition_4(node)
get_attrs(node)
get_new_init_args(matched_nodes)
get_new_vi_args(matched_nodes)
pattern_condition_checker(nodes_to_check)
pattern_to_match = ['Gemm']
weight_transformation(w_arr, **kwargs)
class furiosa.quantizer.frontend.onnx.transformer.fuse_conv.Pattern_3(model)

Bases: furiosa.quantizer.frontend.onnx.transformer.ONNXTransformer

transform

prev –> Conv –> Add –> next

to

prev –> Conv –> next

if len(Conv.input) == 2

make_initializers(base_node)
make_nodes(top_node, base_node)
pattern_condition_checker(nodes_to_check)
pattern_matching(base_node)
pattern_to_match = ['Conv', 'Add']

furiosa.quantizer.frontend.onnx.transformer.fuse_depth_to_space module

class furiosa.quantizer.frontend.onnx.transformer.fuse_depth_to_space.FuseDepthToSpace(*args, **kwds)

Bases: furiosa.quantizer.interfaces.transformer.Transformer

transform(model: onnx.onnx_ml_pb2.ModelProto) onnx.onnx_ml_pb2.ModelProto
class furiosa.quantizer.frontend.onnx.transformer.fuse_depth_to_space.Pattern_1(model)

Bases: furiosa.quantizer.frontend.onnx.transformer.ONNXTransformer

transform

prev –> Reshape –> Transpose –> Reshape –> next

to

prev –> DepthToSpace –> next

if Transpose.perm == [0, 1, 4, 2, 5, 3] or == [0, 3, 4, 1, 5, 2]

get_attrs(top_node, mid_node)
pattern_condition_checker(nodes_to_check)
pattern_matching(base_node)

furiosa.quantizer.frontend.onnx.transformer.fuse_gather_matmul module

class furiosa.quantizer.frontend.onnx.transformer.fuse_gather_matmul.FuseGatherMatMul(*args, **kwds)

Bases: furiosa.quantizer.interfaces.transformer.Transformer

transform(model: onnx.onnx_ml_pb2.ModelProto) onnx.onnx_ml_pb2.ModelProto
class furiosa.quantizer.frontend.onnx.transformer.fuse_gather_matmul.Pattern_1(model)

Bases: furiosa.quantizer.frontend.onnx.transformer.ONNXTransformer

transform

prev –> Gather –> MatMul –> next

to

prev –> Gather –> next

if 1. MatMul.ndim == 2
  1. MatMul must have exactly one initializer

check_condition_1(tensor_name)
check_condition_2(node)
get_new_init_args(matched_nodes)
make_initializers(top_node_init, base_node_init)
make_nodes(matched_nodes)
pattern_condition_checker(nodes_to_check)
pattern_matching(base_node)
pattern_to_match = ['Gather', 'MatMul']

furiosa.quantizer.frontend.onnx.transformer.fuse_gelu module

class furiosa.quantizer.frontend.onnx.transformer.fuse_gelu.BertOnnxModel(model)

Bases: onnxruntime.transformers.onnx_model.OnnxModel

fuse_gelu()
class furiosa.quantizer.frontend.onnx.transformer.fuse_gelu.FuseGELU(*args, **kwds)

Bases: furiosa.quantizer.interfaces.transformer.Transformer

from:
Input –> Div –> Erf –> Add –> M

——————> Mul –> ul–> Output

to:

GELU

transform(model: onnx.onnx_ml_pb2.ModelProto) onnx.onnx_ml_pb2.ModelProto

furiosa.quantizer.frontend.onnx.transformer.fuse_layer_normalization module

class furiosa.quantizer.frontend.onnx.transformer.fuse_layer_normalization.BertOnnxModel(model)

Bases: onnxruntime.transformers.onnx_model.OnnxModel

fuse_layer_normalization()
class furiosa.quantizer.frontend.onnx.transformer.fuse_layer_normalization.FuseLayerNormalization(*args, **kwds)

Bases: furiosa.quantizer.interfaces.transformer.Transformer

from:
Input –> ReduceMean –> S –> Pow –> ReduceMean –> Add –> Sqrt –> D

—————–> ub —————————————–> iv –> Mul –> Add Output

to:

LayerNormalization

transform(model: onnx.onnx_ml_pb2.ModelProto) onnx.onnx_ml_pb2.ModelProto

furiosa.quantizer.frontend.onnx.transformer.fuse_lp_normalization module

class furiosa.quantizer.frontend.onnx.transformer.fuse_lp_normalization.FuseLpNormalization(*args, **kwds)

Bases: furiosa.quantizer.interfaces.transformer.Transformer

transform(model: onnx.onnx_ml_pb2.ModelProto) onnx.onnx_ml_pb2.ModelProto
class furiosa.quantizer.frontend.onnx.transformer.fuse_lp_normalization.Pattern_1(model)

Bases: furiosa.quantizer.frontend.onnx.transformer.ONNXTransformer

transform
prev –> ReduceL2/ReduceL1 –> Clip –> Expand –> Div –> next

——————————————–>

to

prev –> LpNormalization –> next

get_attrs(node)
pattern_matching(base_node)

furiosa.quantizer.frontend.onnx.transformer.fuse_pad module

class furiosa.quantizer.frontend.onnx.transformer.fuse_pad.FusePad(*args, **kwds)

Bases: furiosa.quantizer.interfaces.transformer.Transformer

transform(model: onnx.onnx_ml_pb2.ModelProto) onnx.onnx_ml_pb2.ModelProto
class furiosa.quantizer.frontend.onnx.transformer.fuse_pad.Pattern_1(model)

Bases: furiosa.quantizer.frontend.onnx.transformer.ONNXTransformer

transform

prev –> Pad –> MaxPool –> next

to

prev –> MaxPool –> next

if 1. Pad.mode == ‘constant’
  1. Pad.constant_value == -inf

  2. padded on spatial dimension

  3. fused_pads[i] < kernel_shape[i] and fused_pads[i + kernel_rank] < kernel_shape[i] for all i

check_condition_1(node_attr)
check_condition_2(node)
check_condition_3(pads_input)
check_condition_6(node_attrs, pad_input)
get_attrs(node)
get_pad_mode(node_attr)
make_maxpool_pad(pad_input)
make_new_node(matched_nodes)
pattern_condition_checker(nodes_to_check)
pattern_matching(base_node)
pattern_to_match = ['Pad', 'MaxPool']
update_attrs(attrs, pad_input)
class furiosa.quantizer.frontend.onnx.transformer.fuse_pad.Pattern_2(model)

Bases: furiosa.quantizer.frontend.onnx.transformer.fuse_pad.Pattern_1

transform

prev –> Pad –> AveragePool –> next

to

prev –> AveragePool –> next

if 1. Pad.mode == ‘constant’
  1. Pad.constant_value == 0.0

  2. padded on spatial dimension

  3. AveragePool.count_include_pad == 1 or all AveragePool.pads == 0

  4. AveragePool.ceil_mode == 0

  5. fused_pads[i] < kernel_shape[i] and fused_pads[i + kernel_rank] < kernel_shape[i] for all i

check_condition_2(node)
check_condition_4(node)
check_condition_5(node)
get_attrs(node)
make_new_node(matched_nodes)
pattern_condition_checker(matched_nodes)
pattern_to_match = ['Pad', 'AveragePool']
update_attrs(attrs, pad_input)

furiosa.quantizer.frontend.onnx.transformer.fuse_redundant_reshape_pattern module

class furiosa.quantizer.frontend.onnx.transformer.fuse_redundant_reshape_pattern.FuseRedundantReshapePattern(*args, **kwds)

Bases: furiosa.quantizer.interfaces.transformer.Transformer

transform(model: onnx.onnx_ml_pb2.ModelProto) onnx.onnx_ml_pb2.ModelProto
class furiosa.quantizer.frontend.onnx.transformer.fuse_redundant_reshape_pattern.Pattern_1(model)

Bases: furiosa.quantizer.frontend.onnx.transformer.ONNXTransformer

transform

prev –> Reshape –> Reshape –> next

to

prev –> Reshape –> next

if prev.output[0].shape != next.input[0].shape

make_new_init(matched_nodes)
make_new_node(matched_nodes)
make_new_vi(matched_nodes)
pattern_condition_checker(nodes_to_check)
pattern_matching(base_node)
pattern_to_match = ['Reshape', 'Reshape']
postfix = '_reshape_fused'
class furiosa.quantizer.frontend.onnx.transformer.fuse_redundant_reshape_pattern.Pattern_2(model)

Bases: furiosa.quantizer.frontend.onnx.transformer.fuse_redundant_reshape_pattern.Pattern_1

transform

prev –> Reshape –> Reshape –> Reshape –> next

to

prev –> Reshape –> next

if prev.output[0].shape != next.input[0].shape

pattern_to_match = ['Reshape', 'Reshape', 'Reshape']
class furiosa.quantizer.frontend.onnx.transformer.fuse_redundant_reshape_pattern.Pattern_3(model)

Bases: furiosa.quantizer.frontend.onnx.transformer.fuse_redundant_reshape_pattern.Pattern_1

transform

prev –> Flatten/Squeeze –> Unsqueeze –> next

to

prev –> Reshape –> next

if prev.output[0].shape != next.input[0].shape

make_new_init(matched_nodes)
make_new_node(matched_nodes)
make_new_vi(matched_nodes)
pattern_to_match = ['Flatten/Squeeze', 'Unsqueeze']

furiosa.quantizer.frontend.onnx.transformer.polish_model module

class furiosa.quantizer.frontend.onnx.transformer.polish_model.PolishModel(input_shapes: Optional[Dict[str, List[int]]] = None)

Bases: furiosa.quantizer.interfaces.transformer.Transformer[onnx.onnx_ml_pb2.ModelProto]

Essential graph transformer/optimizers

transform(model: onnx.onnx_ml_pb2.ModelProto) onnx.onnx_ml_pb2.ModelProto

furiosa.quantizer.frontend.onnx.transformer.utils module

furiosa.quantizer.frontend.onnx.transformer.utils.eliminate_unused_initializer(model)
furiosa.quantizer.frontend.onnx.transformer.utils.eliminate_unused_input(model)
furiosa.quantizer.frontend.onnx.transformer.utils.eliminate_unused_output(model)
furiosa.quantizer.frontend.onnx.transformer.utils.eliminate_unused_protos(model)
furiosa.quantizer.frontend.onnx.transformer.utils.eliminate_unused_value_info(model)
furiosa.quantizer.frontend.onnx.transformer.utils.fix_batch_size_as_one(model)

fix batch_size = 1 if dim_param is given.

furiosa.quantizer.frontend.onnx.transformer.utils.include_initializer_to_graph_input(model)
furiosa.quantizer.frontend.onnx.transformer.utils.make_conv_bias_name_unique(model)
furiosa.quantizer.frontend.onnx.transformer.utils.name_nodes(model)
furiosa.quantizer.frontend.onnx.transformer.utils.rebuild_model(model, new_nodes, eliminate=True, renaming=True)

Module contents

class furiosa.quantizer.frontend.onnx.transformer.ONNXTransformer(model)

Bases: object

bridge_disconnected_nodes(node_0: onnx.onnx_ml_pb2.NodeProto, next_nodes: List[onnx.onnx_ml_pb2.NodeProto], new_input)
For a graph changed, for example,

before) prev –> node_1 –> node_0 –> next after) prev –> node_1 –> ( ) -/-> next

This function bridges node_1 and next as follows:

prev –> node_1 –> next by assigning next.input[y] = node_1.output[x]

build_optimized_model(model)
copy_value_info(name)
find_next_node(node: onnx.onnx_ml_pb2.NodeProto) List[onnx.onnx_ml_pb2.NodeProto]
find_prev_node(node_input: str) onnx.onnx_ml_pb2.NodeProto
get_data_node_input(node)
get_init_node_input(node)
get_initializer_array(node_input)
get_map_values(field)
get_value_info_shape(value_info_name: str) List[int]
is_op_type(op_type: str, target_op_types: List[str])
is_same_shape(input_1, input_2)
make_field_unique(values)
make_initializer_from_array(array: numpy.array, name: Optional[str] = None) onnx.onnx_ml_pb2.TensorProto
make_int64_initializer(name, target_name)
make_node(op_type, inputs, outputs, name=None, **attrs)
make_tensor_value_info(name, elem_type, shape)
pattern_condition_checker(nodes_to_check)
pattern_matcher(node, pattern_to_match: List[str])
pattern_matching(node)
pop_multiple_initializer_map(nodes: List[onnx.onnx_ml_pb2.TensorProto])
pop_multiple_optimizer_map(nodes: List[onnx.onnx_ml_pb2.NodeProto])
pop_multiple_value_info_map(vis: List[onnx.onnx_ml_pb2.ValueInfoProto])
pop_single_initializer_map(init: onnx.onnx_ml_pb2.TensorProto)
pop_single_optimizer_map(node: onnx.onnx_ml_pb2.NodeProto)
pop_single_value_info_map(vi: onnx.onnx_ml_pb2.NodeProto)
transform()
transform_to_convert(nodes_to_remove: List[onnx.onnx_ml_pb2.NodeProto], nodes_to_add: Optional[List[onnx.onnx_ml_pb2.NodeProto]] = None, inits_to_add: Optional[List[onnx.onnx_ml_pb2.TensorProto]] = None, vis_to_add: Optional[List[onnx.onnx_ml_pb2.ValueInfoProto]] = None)
transform_to_eliminate(nodes_to_remove: List[onnx.onnx_ml_pb2.NodeProto], new_input)
transform_to_fuse(nodes_to_remove: List[onnx.onnx_ml_pb2.NodeProto], nodes_to_add: Optional[List[onnx.onnx_ml_pb2.NodeProto]] = None, inits_to_add: Optional[List[onnx.onnx_ml_pb2.TensorProto]] = None, vis_to_add: Optional[List[onnx.onnx_ml_pb2.ValueInfoProto]] = None)
traverse_prev_node(producer_map_key: str, target_op_types: List[str])
update_graph_fields(model)
update_multiple_initializer_map(initializers: List[onnx.onnx_ml_pb2.TensorProto])
update_multiple_optimizer_map(nodes: List[onnx.onnx_ml_pb2.NodeProto], dest_name)
update_multiple_value_info_map(value_infos: List[onnx.onnx_ml_pb2.ValueInfoProto])
update_single_initializer_map(initializer: onnx.onnx_ml_pb2.TensorProto)
update_single_optimizer_map(node: onnx.onnx_ml_pb2.NodeProto, dest_name)
update_single_value_info_map(value_info: onnx.onnx_ml_pb2.ValueInfoProto)