yobx.tensorflow.ops.matmul#

Converter for the TF MatMul / BatchMatMulV2 / BatchMatMul op → ONNX MatMul.

Matrix multiplication#

MatMul, BatchMatMulV2, BatchMatMul

All three TF op variants produce standard matrix multiplication (or batched matrix multiplication) and map directly to a single ONNX MatMul node.

Transposition / adjoint#

TF supports two families of “reverse-input” attributes that indicate the operands should be transposed before multiplying:

  • transpose_a / transpose_b — used by 2-D MatMul

  • adj_x / adj_y — used by BatchMatMulV2 and BatchMatMul

For real-valued tensors the adjoint is identical to the transpose, so both families are handled identically: when an attribute is True an ONNX Transpose node that swaps the last two dimensions is inserted before the MatMul.

Because the batch dimensions can be of arbitrary rank the permutation is computed dynamically by _transpose_last_two() rather than using hard-coded indices like [-1, -2].

Example

The following TF graph fragment:

# batched matmul with transposed B: result[b, i, j] = sum_k A[b, i, k] * B[b, j, k]
tf.matmul(A, B, adjoint_b=True)   # BatchMatMulV2 with adj_y=True

is converted to:

Transpose(B, perm=[0, 2, 1])   # swap last two dims
MatMul(A, transposed_B)
yobx.tensorflow.ops.matmul.convert_matmul(g: GraphBuilderExtendedProtocol, sts: Dict[str, Any], outputs: List[str], op: Operation) str[source]#

Converts TF MatMul / BatchMatMulV2 / BatchMatMul → ONNX MatMul.

Parameters:
  • g – the active GraphBuilder instance.

  • sts – metadata dictionary (unused, always {}).

  • outputs – list of pre-allocated output tensor names; outputs[0] receives the result of the multiplication.

  • op – the TF MatMul, BatchMatMulV2, or BatchMatMul operation. Its first two inputs are the left and right operands.

Returns:

the name of the primary output tensor.

TF’s transpose flags (transpose_a / transpose_b) and adjoint flags (adj_x / adj_y used by BatchMatMulV2) are honoured by inserting ONNX Transpose nodes when needed. For real-valued tensors, adjoint is equivalent to transpose.

The permutation used for transposition is always rank-aware: it swaps only the last two axes so that arbitrary batch dimensions are left intact. See _transpose_last_two() for details.