Tensor

class onnx_graphsurgeon.Tensor

Bases: object

Abstract base class for tensors in a graph

This class is abstract and cannot be constructed directly.

is_empty()

Returns whether this tensor is considered empty in the graph.

Note: ‘Empty’ here refers to the name of the tensor, which is omitted for optional tensors, NOT the shape of the tensor

Returns

Whether the tensor is empty, meaning that it is used for an omitted optional input or output.

Return type

bool

to_constant(values: numpy.ndarray, data_location: Optional[int] = None)

Modifies this tensor in-place to convert it to a Constant. This means that all consumers/producers of the tensor will see the update.

Parameters
  • values (np.ndarray) – The values in this tensor

  • data_location (int) – An enum value indicating the location where the tensor data is stored. Generally, this will come from onnx.TensorProto.DataLocation.

Returns

self

to_variable(dtype: Optional[numpy.dtype] = None, shape: Sequence[Union[int, str]] = [])

Modifies this tensor in-place to convert it to a Variable. This means that all consumers/producers of the tensor will see the update.

Parameters
  • dtype (np.dtype) – The data type of the tensor.

  • shape (Sequence[int]) – The shape of the tensor.

Returns

self

i(tensor_idx=0, producer_idx=0)

Convenience function to get an input tensor of one of this tensor’s input nodes. Note that the parameters are swapped compared to the o() function; this is because tensors are likely to have only a single producer

For example:

assert tensor.i() == tensor.inputs[0].inputs[0]
assert tensor.i(1, 2) == tensor.inputs[2].inputs[1]
Parameters
  • tensor_idx (int) – The index of the input tensor of the input node. Defaults to 0.

  • producer_idx (int) – The index of the producer node of the input tensor, if the tensor has multiple producers. Defaults to 0.

Returns

The specified producer (input) tensor.

Return type

Tensor

o(consumer_idx=0, tensor_idx=0)

Convenience function to get an output tensor of one of this tensor’s output nodes.

For example:

assert tensor.o() == tensor.outputs[0].outputs[0]
assert tensor.o(2, 1) == tensor.outputs[2].outputs[1]
Parameters
  • consumer_idx (int) – The index of the consumer of the input tensor. Defaults to 0.

  • tensor_idx (int) – The index of the output tensor of the node, if the node has multiple outputs. Defaults to 0.

Returns

The specified consumer (output) tensor

Return type

Tensor