RiptideRiptide
Download Riptide SDKInstall Riptorch
Menu
Raya docs · Geometry, physics and speed

The CPU matrix unit

Matrix multiply on the matrix unit (SME on M4, M5 and A18; a portable path elsewhere) for float32, exact int8 → int32 and int16 → int64.

import 'package:riptide_matrix/riptide_matrix.ray';
1 classes5 functions and methods6 of 6 documented3 runnable examples

Overview

Matrix multiply on the CPU's matrix unit from Raya: Matrix.multiplyF32, multiplyI8 (int8 -> int32, exact) and multiplyI16 (int16 -> int64, exact). C = A · op(B), row-major; bRows: true takes B as the product's columns stored as rows (C = A · Bᵀ, the Gram matrix X · Xᵀ with B = A).

WhereWhat runs
Raya VM on a CPU with Arm SME (Apple M4/M5, A18 and later)the matrix unit: outer products into the ZA tiles (FMOPA, SMOPA; int16 -> int64 needs FEAT_SME_I16I64)
Raya VM without SMEthe VM's portable loop
older VMs and enginesthis package's Raya loop

Matrix.features says what the CPU matrix unit does here (bit 0 f32, 1 int8, 2 int16 -> int64). Lengths are checked before any native call.

Measured on an M5 Pro, one thread, all-pairs Gram 4,096 x 128 (16.8 M pairs): f32 1.30 TFLOPS (0.196 ns per pair), int8 2.20 TOPS exact (0.116 ns), int16 -> int64 1.37 TOPS exact (0.187 ns). For short int8 dot products inside your own loops, riptide_simd's Int32x4.sdot/smmla are the register-level tools.

Evelyn apps get the native path once the engine they link carries and exports raya_*.

Classes

class

Matrix

abstract final class Matrix

Matrix multiply entry points. See the library comment.

Static

featuresstaticgetter
static int get features

What the CPU matrix unit does here: bit 0 f32, bit 1 int8 -> int32, bit 2 int16 -> int64 (0 when the work runs on a portable loop).

nativeAvailablestaticgetter
static bool get nativeAvailable

Whether this VM exports the entry points.

multiplyF32staticmethod
static void multiplyF32(Float32List a, Float32List b, Float32List c, {required int m, required int n, required int k, bool bRows = false})

F32: c = a · op(b); c is m x n.

Example
import 'package:riptide_matrix/riptide_matrix.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // C (2 × 2) = A (2 × 3) · B (3 × 2), row-major.
  final a = Float32List.fromList([1, 2, 3, 4, 5, 6]);
  final b = Float32List.fromList([7, 8, 9, 10, 11, 12]);
  final c = Float32List(4);
  Matrix.multiplyF32(a, b, c, m: 2, n: 2, k: 3);
  print('A·B = $c');
  // bRows: B holds rows to dot with: the Gram matrix A·Aᵀ.
  final g = Float32List(4);
  Matrix.multiplyF32(a, a, g, m: 2, n: 2, k: 3, bRows: true);
  print('A·Aᵀ = $g');
}
Output
A·B = [58.0, 64.0, 139.0, 154.0]
A·Aᵀ = [14.0, 32.0, 32.0, 77.0]
multiplyI8staticmethod
static void multiplyI8(Int8List a, Int8List b, Int32List c, {required int m, required int n, required int k, bool bRows = false})

Int8 -> int32, exact while k < 2^17.

Example
import 'package:riptide_matrix/riptide_matrix.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // int8 inputs, exact int32 sums: a 3 × 4 by 4 × 3 product.
  final a = Int8List.fromList([127, -128, 1, 0, 2, 2, 2, 2, -1, 0, 1, 0]);
  final b = Int8List.fromList([1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1]);
  final c = Int32List(9);
  Matrix.multiplyI8(a, b, c, m: 3, n: 3, k: 4);
  print('C = $c');
  final rows = [for (var i = 0; i < 3; i++) [for (var j = 0; j < 3; j++) c[i * 3 + j]]];
}
Output
C = [127, -128, 1, 4, 4, 4, -1, 0, 1]
multiplyI16staticmethod
static void multiplyI16(Int16List a, Int16List b, Int64List c, {required int m, required int n, required int k, bool bRows = false})

Int16 -> int64, exact (k < 2^33). Exact 15- and 16-bit work in one product instead of four int8 limbs.

Example
import 'package:riptide_matrix/riptide_matrix.ray';
import 'raya:convert';
import 'raya:typed_data';

void main() {
  // int16 inputs, exact int64 sums: 32767² · 4 overflows int32 but not int64.
  final a = Int16List.fromList([32767, 32767, 32767, 32767]);
  final c = Int64List(1);
  Matrix.multiplyI16(a, a, c, m: 1, n: 1, k: 4, bRows: true);
  print('Σ 32767² over 4 terms = ${c[0]}');
  print('as int32 it would wrap: ${c[0] > 2147483647}');
}
Output
Σ 32767² over 4 terms = 4294705156
as int32 it would wrap: true