ComfyUI Node

Pt Mat Mul

The general matrix multiply, batches welcome

By HowToSD·Created about a year ago·Updated about a year ago· 7
Pt Mat Mul
  • tens_a
  • tens_b
  • TENSOR

Pt Mat Mul is torch.matmul - the do-everything matrix multiplication. Two tensors in, their product out, with torch's full set of rules: plain 2D matrix multiply, batched matmul over extra dimensions, even a vector times a matrix. If you're building the pack's Transformer from scratch, this is the node your attention heads are made of.

How it works

Required inputs tens_a and tens_b, both TENSOR, output a single TENSOR. The implementation is literally torch.matmul(tens_a, tens_b), which means you get all of matmul's behavior for free:

  • 2D × 2D - the classic: (m, k) × (k, n)(m, n).
  • Batched - (b, m, k) × (b, k, n)(b, m, n), with broadcasting across batch dimensions.
  • Vector edge cases - a 1D tensor on either side gets promoted and squeezed, so (k) × (k, n)(n).

That's the real difference from Pt Mm, its strict cousin: Mm only accepts exactly-2D inputs and throws a ValueError otherwise. Mat Mul is the forgiving one, and forgiving is what you want in a graph where shapes change between experiments.

The one rule that matters

The inner dimensions must match - tens_a's last dimension has to equal tens_b's second-to-last. Get that wrong and matmul raises a shape error, which is at least explicit. The beginner failure mode is confusing this with Pt Mul: Mat Mul is the matrix product (sum of products, inner-dimension rule applies), Pt Mul is element-wise multiplication (just needs broadcastable shapes). For scaling and weighting you want Pt Mul; for actual linear algebra - attention scores, linear layers, rotations - you want this.

Installing

Pt Mat Mul ships in the HowToSD/ComfyUI-Pt-Wrapper pack under the "Data Analysis" menu. One install, ~200 nodes. ComfyUI Manager: search ComfyUI-Pt-Wrapper, install, restart. Manual:

cd ComfyUI/custom_nodes
git clone https://github.com/HowToSD/ComfyUI-Pt-Wrapper

The pack's requirements list is heavy (transformers, peft, accelerate…) for the training side; matmul needs only PyTorch, already present. Skip the pip line for math-only use. No models to download.

And the standing pack rule: everything speaks the custom TENSOR type, not ComfyUI's IMAGE/LATENT - convert with Pt From Image (Pt From Image Transpose for (b, c, h, w)), back out with Pt To Image.

CategoryData Analysis

Inputs (2)

NameTypeDefaultDescription
tens_aTENSOR
tens_bTENSOR

Outputs (1)

NameTypeDescription
TENSORTENSOR