|
ONE - On-device Neural Engine
|
Data Structures | |
| class | _ModelTransformerHelper |
| class | _TensorInfo |
| class | LegalizeOptions |
Functions | |
| _reverse_str (s) | |
| _parse_tensor_name (name) | |
| _get_tensor_infos (model) | |
| _dtype_to_np (dtype) | |
| _generate_one_direction_RNN (transformer, X, W, R, B, initial_h, clip, activation_name) | |
| _transform_unidirectional_RNN (transformer, original_node, x, tensor_infos, activation, clip, direction, hidden_size, layout) | |
| _transform_bidirectional_RNN (transformer, original_node, x, tensor_infos, activations, clip, hidden_size, layout) | |
| _legalize_RNN (transformer, tensor_infos, node) | |
| _generate_one_direction_LSTM (transformer, X, W, R, B, initial_h, initial_c, P, clip, act, dtype, hidden_size, batch_size) | |
| _transform_unidirectional_LSTM (transformer, original_node, x, tensor_infos, activations, clip, direction, hidden_size, layout) | |
| _transform_bidirectional_LSTM (transformer, original_node, x, tensor_infos, activations, clip, hidden_size, layout) | |
| _legalize_LSTM (transformer, tensor_infos, node) | |
| legalize (model, options) | |
Variables | |
| options = LegalizeOptions() | |
| unroll_lstm | |
| unroll_rnn | |
| model = onnx.load(sys.argv[1]) | |
|
protected |
Convert onnx dtype value to numpy dtype class
For more types see:
https://github.com/onnx/onnx/blob/96516aecd4c110b0ac57eba08ac236ebf7205728/onnx/onnx.proto3#L484
Args:
dtype (int): onnx dtype
Returns:
numpy data type: numpy dtype, like np.float32
Definition at line 328 of file onnx_legalizer.py.
Referenced by _transform_bidirectional_LSTM(), _transform_bidirectional_RNN(), _transform_unidirectional_LSTM(), and _transform_unidirectional_RNN().
|
protected |
Generate subgraph for one direction of unrolled LSTM layer
Args:
transformer (_ModelTransformerHelper): helper for model generation
X (list of str): names of tensors in input sequence. Each tensor shape: [batch_size, input_size]
W (str): name of concatenated weight tensor: [input, output, forget, cell]
R (str): name of concatenated recurrence weights tensor: [input, output, forget, cell]
B (str): name of concatenated bias tensor: [input, output, forget, cell]
initial_h (str or None): name of tensor containing initial hidden state. Shape [batch_size, hidden_size]
initial_c (str or None): name of tensor containing initial cell state. Shape [batch_size, hidden_size]
P (str or None): name of concatenated peephole tensor: [input, output, forget]
clip (float or None): range which clips input of activations
act (dict of str): activation functions {'f': 'Sigmoid', 'g': 'Tanh', 'h': 'Tanh'}
dtype (numpy dtype): data type used in created LSTM operation
hidden_size (int): hidden dimension
batch_size (int): batch dimension
Definition at line 621 of file onnx_legalizer.py.
Referenced by _transform_bidirectional_LSTM(), and _transform_unidirectional_LSTM().
|
protected |
Generate subgraph of one direction of unrolled RNN layer
Args:
transformer (_ModelTransformerHelper): helper for model generation
X (list of str): names of input tensors in sequence. Tensor shapes: [batch_size, input_size].
W (str): name of weight tensor
R (str): name of recurrence weight tensor
B (str): name of bias tensor
initial_h (str or None): name of tensor containing initial hidden state. Shape [batch_size, hidden_size]
clip (float or None): range which clips input of activations
act (str): activation function
Definition at line 347 of file onnx_legalizer.py.
Referenced by _transform_bidirectional_RNN(), and _transform_unidirectional_RNN().
|
protected |
Infer tensor shapes and dtypes
Args:
model (onnx.onnx_ml_pb2.ModelProto): model to process
Returns:
dict from str to _TensorInfo: maps tensor name to shape and dtype information
Definition at line 304 of file onnx_legalizer.py.
Referenced by legalize().
|
protected |
Unroll LSTM operation
Args:
transformer (_ModelTransformerHelper): transformation helper
tensor_infos (dict from str to _TensorInfo): dict maps tensor name to it's shape and dtype info
node (onnx.onnx_ml_pb2.NodeProto): LSTM operation to unroll
Definition at line 947 of file onnx_legalizer.py.
References _transform_bidirectional_LSTM(), and _transform_unidirectional_LSTM().
Referenced by legalize().
|
protected |
Unroll RNN operation
Args:
transformer (_ModelTransformerHelper): transformation helper
tensor_infos (dict from str to _TensorInfo): dict maps tensor name to it's shape and dtype info
node (onnx.onnx_ml_pb2.NodeProto): RNN operation to unroll
Definition at line 552 of file onnx_legalizer.py.
References _transform_bidirectional_RNN(), and _transform_unidirectional_RNN().
Referenced by legalize().
|
protected |
Splits tensor name to base part and serial number
Most of tensor names have following format: "tensor_123".
This function breaks name into two values: "tensor_" and 123.
Tensor names like this: "321" are broken into "" and 321.
Serial number is used to create unique tensor names using given base name.
Args:
name (str): tensor name
Returns:
tuple of str, int: base name and serial number of tensor
Definition at line 52 of file onnx_legalizer.py.
References _reverse_str().
|
protected |
Definition at line 48 of file onnx_legalizer.py.
Referenced by _parse_tensor_name().
|
protected |
Generate Bidirectional unrolled LSTM
Args:
transformer (_ModelTransformerHelper): transformation helper
original_node (onnx.onnx_ml_pb2.NodeProto): bidirectional LSTM operation to unroll
x (list of str): list of input tensors (input tensor split along "time" dimension)
tensor_infos (dict from str to _TensorInfo): dict maps tensor name to it's shape and dtype info
activations (list of str): list of length 6, containing names of forward and reverse activations
clip (float or None): range which clips input of activations
hidden_size (int): size of hidden state
layout (int): See attribute description:
https://github.com/onnx/onnx/blob/5cf5feef5ec3fd5527b2fdb6c29780e3b705059f/docs/Operators.md#attributes-37
Definition at line 837 of file onnx_legalizer.py.
References _dtype_to_np(), and _generate_one_direction_LSTM().
Referenced by _legalize_LSTM().
|
protected |
Generate Bidirectional unrolled RNN
Args:
transformer (_ModelTransformerHelper): transformation helper
original_node (onnx.onnx_ml_pb2.NodeProto): bidirectional RNN operation to unroll
x (list of str): list of input tensors (input tensor split along "time" dimension)
tensor_infos (dict from str to _TensorInfo): dict maps tensor name to it's shape and dtype info
activations (list of str): list of len (2) containing names of forward and reverse activations
clip (float or None): range which clips input of activations
hidden_size (int): size of hidden state
layout (int): See attribute description:
https://github.com/onnx/onnx/blob/5cf5feef5ec3fd5527b2fdb6c29780e3b705059f/docs/Operators.md#attributes-56
Definition at line 460 of file onnx_legalizer.py.
References _dtype_to_np(), and _generate_one_direction_RNN().
Referenced by _legalize_RNN().
|
protected |
Generate Simple (forward or reverse) unrolled LSTM
Args:
transformer (_ModelTransformerHelper): transformation helper
original_node (onnx.onnx_ml_pb2.NodeProto): unidirectional LSTM operation to unroll
x (list of str): list of input tensors (input tensor split along "time" dimension)
tensor_infos (dict from str to _TensorInfo): dict maps tensor name to it's shape and dtype info
activations (list of str): list of length 3 containing names of activation functions
clip (float or None): range which clips input of activations
direction (str): "forward" or "reverse"
hidden_size (int): size of hidden state
layout (int): See attribute description:
https://github.com/onnx/onnx/blob/5cf5feef5ec3fd5527b2fdb6c29780e3b705059f/docs/Operators.md#attributes-37
Definition at line 765 of file onnx_legalizer.py.
References _dtype_to_np(), and _generate_one_direction_LSTM().
Referenced by _legalize_LSTM().
|
protected |
Generate Simple (forward or reverse) unrolled RNN
Args:
transformer (_ModelTransformerHelper): transformation helper
original_node (onnx.onnx_ml_pb2.NodeProto): unidirectional RNN operation to unroll
x (list of str): list of input tensors (input tensor split along "time" dimension)
tensor_infos (dict from str to _TensorInfo): dict maps tensor name to it's shape and dtype info
activation (str): name of activation function
clip (float or None): range which clips input of activations
direction (str): "forward" or "reverse"
hidden_size (int): size of hidden state
layout (int): See attribute description:
https://github.com/onnx/onnx/blob/5cf5feef5ec3fd5527b2fdb6c29780e3b705059f/docs/Operators.md#attributes-56
Definition at line 403 of file onnx_legalizer.py.
References _dtype_to_np(), and _generate_one_direction_RNN().
Referenced by _legalize_RNN().
| onnx_legalizer.legalize | ( | model, | |
| options | |||
| ) |
Replace selected operations in onnx model
Replaces operations, selected by given options with different operation sequences.
For example remove unsupported parts of graph with sequences of supported operations.
Note that graph is changes inplace.
Args:
model (onnx.onnx_ml_pb2.ModelProto): target model
options (LegalizeOptions):
Definition at line 1022 of file onnx_legalizer.py.
References _get_tensor_infos(), _legalize_LSTM(), and _legalize_RNN().
| onnx_legalizer.model = onnx.load(sys.argv[1]) |
Definition at line 1073 of file onnx_legalizer.py.
| onnx_legalizer.options = LegalizeOptions() |
Definition at line 1070 of file onnx_legalizer.py.
| onnx_legalizer.unroll_lstm |
Definition at line 1071 of file onnx_legalizer.py.
| onnx_legalizer.unroll_rnn |
Definition at line 1072 of file onnx_legalizer.py.