RiptideRiptide
Download Riptide SDKInstall Riptorch
Menu
Raya docs · Numerical computing

Tensors

A tensor API that records a graph, runs it on the CPU while you develop, and compiles it to GPU kernels. Riptorch’s engine grew from here.

import 'package:riptide_tensor/riptide_tensor.ray';
import 'package:riptide_tensor/hook.ray';
23 classes130 functions and methods90 of 262 documented89 runnable examples

Overview

Record a graph in Raya, run it on the CPU now, replay tinygrad's GPU kernels for it in release.

  • Tensor: tinygrad's core ops (elementwise, reduce, movement, matmul, conv2d, arange) are recorded as graph nodes, plus scaledDotProductAttention as one attention node; everything else (relu, exp, softmax, cat...) is built from them the way tinygrad builds it. matmul / conv2d take an optional MatmulPrecision for library GEMMs.
  • CpuInterpreter / CpuTensorBackend: runs the graph in plain Raya (float32 rounding per op, 32-bit integer wrap). The debug and hot-restart path, and the reference every GPU program is checked against.
  • TensorProgram: the program file tinygrad writes (GLSL and MSL kernels, library steps with their fallback kernels, buffer plan, launch dims, weights).
  • TensorBackend: the interface backends implement (riptide_tensor_gpu: Riptide and Evelyn GPU).
  • Library mode (buildTensorPrograms(library: Ops.library)): tinygrad leaves matmul, conv-as-GEMM and attention unfused for own kernels (package:riptide/kernels.ray).
  • package:riptide_tensor/hook.ray: buildTensorProgramsHook for a package's hook/build.ray.
  • ProgramCompiler: what turns a graph into a program. TinygradToolchain is the default; the Riptide compiler implements it in process and is picked with RIPTIDE_COMPILER=riptide|both through package:riptide_compiler/hook.ray.

Functions and constants

Functions

Future<List<TensorProgramBuild>> buildTensorProgramsHook(BuildInput input, BuildOutputBuilder output, Map<String, TensorGraph> graphs, {int beam = 0, String? tuneDevice, String family = 'generic', Set<String> library = const {}, MatmulPrecision libraryPrecision = MatmulPrecision.f16, int libraryMinFlops = 1 << 20, bool msl = true})
String? locateImpellerc({Map<String, String>? environment})
Future<List<TensorProgramBuild>> buildTensorPrograms({required Directory outDir, required Map<String, TensorGraph> graphs, required ProgramCompiler? toolchain, int beam = 0, String? tuneDevice, String family = 'generic', bool keepGoing = false, Set<String> library = const {}, MatmulPrecision libraryPrecision = MatmulPrecision.f16, int libraryMinFlops = 1 << 20, bool msl = true})

library (any of Ops.library) builds library steps: tinygrad leaves those nodes unfused and the program names them for own kernels (Metal 4 tensor-op GEMM in libraryPrecision, fused attention), with tinygrad's kernels for each as its fallback; GEMMs under libraryMinFlops stay fused. msl also writes every kernel in Riptide MSL. "Library kernels".

The cache key is the graph's hash, the tinygrad commit and the tuning options, kept in <name>.stamp. With no toolchain the graph is skipped, never failed: debug builds and hot restarts run the graph on the CPU, and a release build with a stale program falls back to the CPU too (the runtime compares TensorProgram.graphHash).

Future<void> compileShaderBundle(TensorProgram program, File bundle, String impellerc)

Constants and variables

const maxKernelBuffers = 30

The most buffers one generated kernel may bind (inputs + output): Metal's 31 argument-table slots, minus one spare. MAX_KERNEL_BUFFERS in the environment overrides it (lower for Vulkan devices with fewer storage-buffer bindings).

Classes

abstract final class TensorContainer

The container both riptide_tensor files use (the graph Raya records, the program tinygrad writes back):

Magic (4 bytes) | u32 version | u32 json length | json (utf-8) | zero pad to 16 | blob

Little endian. Offsets in the json are relative to the blob's start.

Static

graphMagicconstant
static const graphMagic = 'STG1'
programMagicconstant
static const programMagic = 'STP1'
versionconstant
static const version = 1
packstaticmethod
static Uint8List pack(String magic, Map<String, Object?> header, [Uint8List? blob])
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final bytes = TensorContainer.pack('DEMO', {'name': 'w', 'shape': [2, 2]}, Uint8List.fromList([1, 2, 3, 4]));
  print('${bytes.length} bytes, magic ${String.fromCharCodes(bytes.sublist(0, 4))}');
}
Output
52 bytes, magic DEMO
unpackstaticmethod
static (Map<String, Object?>, Uint8List) unpack(Uint8List data, String magic)

The header and the blob (a view into data, no copy).

Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final bytes = TensorContainer.pack('DEMO', {'answer': 42}, Uint8List.fromList([9, 8, 7]));
  final (header, blob) = TensorContainer.unpack(bytes, 'DEMO');
  print('header $header, blob $blob');
}
Output
header {answer: 42}, blob [9, 8, 7]
final class CpuInterpreter

Runs a TensorGraph on the CPU in plain Raya: the reference every GPU program is checked against, and the path debug builds and hot restarts take, so development never waits on a tinygrad build.

Each node becomes a contiguous typed list (float32 results are rounded to float32 after every op, as on the GPU; uint32/int32 wrap at 32 bits). A node's list is freed after its last use.

Constructors

CpuInterpreterconstructor
CpuInterpreter(this.graph)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final a = Tensor.input(g, 'a', [3]);
  (a * a + 1).output('y');
  final out = CpuInterpreter(g).run({'a': Float32List.fromList([1, 2, 3])});
  print('y = ${out['y']}');
}
Output
y = [2.0, 5.0, 10.0]

Static

allocstaticmethod
static TypedData alloc(DType dtype, int n)

Allocates the typed list for n elements of dtype.

Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  print(CpuInterpreter.alloc(DType.float32, 3).runtimeType == Float32List);
  print(CpuInterpreter.alloc(DType.int32, 2));
}
Output from the Raya VM
false
[0, 0]

Properties

graphfield
final TensorGraph graph

Methods

runmethod
Map<String, TypedData> run(Map<String, TypedData> inputs)

Runs the graph. inputs are typed lists (Float32List for float32, Int32List, Uint32List) in row-major order.

Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.input(g, 'x', [4]);
  x.sum().output('total');
  x.max().output('largest');
  final out = CpuInterpreter(g).run({'x': Float32List.fromList([3, 1, 4, 1])});
  print('total ${out['total']}, largest ${out['largest']}');
}
Output
total [9.0], largest [4.0]
enum

DType

enum DType

Element types. GPU programs store every type in 32 bits; bool exists inside kernels and on the CPU only.

Values

float32value
float32('float32')
int32value
int32('int32')
uint32value
uint32('uint32')
bool_value
bool_('bool')

Constructors

DTypeconstructor
const DType(this.wire)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  for (final t in DType.values) {
    print('${t.name.padRight(8)} wire "${t.wire}", float ${t.isFloat}, int ${t.isInt}');
  }
}
Output
float32  wire "float32", float true, int false
int32    wire "int32", float false, int true
uint32   wire "uint32", float false, int true
bool_    wire "bool", float false, int false

Static

fromWirestaticmethod
static DType fromWire(String name)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  print(DType.fromWire('int32'));
  print(DType.fromWire('bool'));
}
Output
DType.int32
DType.bool_

Properties

wirefield
final String wire

The name in the graph and program files (tinygrad's dtype names).

isFloatgetter
bool get isFloat
isIntgetter
bool get isInt
class

Node

final class Node

One recorded op. Ops and their arguments mirror tinygrad; CpuInterpreter defines what each computes.

Properties

idfield
final int id
opfield
final String op
shapefield
final List<int> shape
dtypefield
final DType dtype
srcfield
final List<Node> src
argsfield
final Map<String, Object?> args
numelgetter
int get numel

Methods

toJsonmethod
Map<String, Object?> toJson()
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final t = Tensor.input(g, 'x', [2]) * 2;
  print(t.node.toJson());
}
Output
{id: 2, op: mul, shape: [2], dtype: float32, src: [0, 1]}
class

Ops

abstract final class Ops

Ops a TensorGraph records.

Static

leavesconstant
static const leaves = {'input', 'weight', 'const', 'arange'}
unaryconstant
static const unary = {'neg', 'recip', 'sqrt', 'exp2', 'log2', 'sin', 'trunc'}
binaryconstant
static const binary
compareconstant
static const compare = {'cmplt', 'cmpne', 'cmpeq'}
reduceconstant
static const reduce = {'sum', 'rmax'}
movementconstant
static const movement = {'reshape', 'permute', 'expand', 'pad', 'shrink', 'flip'}
otherconstant
static const other = {'cast', 'bitcast', 'where', 'matmul', 'conv2d', 'attention'}
libraryconstant
static const library = {'matmul', 'conv2d', 'attention'}

Ops a program built in library mode can hand to own kernels (a library step) instead of tinygrad's.

enum MatmulPrecision

The operand format a library GEMM may use for a matmul or conv2d node (library mode only; tinygrad's own kernels and the CPU interpreter always compute float32). Accumulation is float32 in every case.

Values

f32value
f32('f32')

Float32 operands on the matrix units' float32 path.

f16value
f16('f16')

Float16 operands (about 3.5x float32 on M5-class GPUs; error ~2.6e-4 relative).

bf16value
bf16('bf16')

Bfloat16 operands (float32's range, 8-bit mantissa; ~2.1e-3).

f32x3value
f32x3('f32x3')

Float32 as two float16 halves and three float16 products: float32 accuracy (or better) at float32 speed or better; values must stay inside float16's range.

Constructors

MatmulPrecisionconstructor
const MatmulPrecision(this.wire)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  for (final p in MatmulPrecision.values) {
    print('${p.name}: wire ${p.wire}');
  }
}
Output
f32: wire f32
f16: wire f16
bf16: wire bf16
f32x3: wire f32x3

Properties

wirefield
final String wire
final class TensorGraph

A recorded computation: inputs, weights, ops, named outputs. Build one with Tensors (tensor.ray), then run it on the CPU (CpuInterpreter), or compile it with tinygrad (buildTensorPrograms) and replay it on the GPU.

Constructors

TensorGraphconstructor
TensorGraph(this.name)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.input(g, 'x', [2, 2]);
  (x.matmul(x) + x).relu().output('y');
  print('graph "${g.name}" has ${g.nodes.length} nodes');
}
Output
graph "demo" has 5 nodes

Properties

namefield
final String name
nodesfield
final List<Node> nodes
outputsfield
final Map<String, Node> outputs
inputsfield
final Map<String, Node> inputs
weightsfield
final Map<String, Uint8List> weights
hashgetter
String get hash

Sha256 of toBytes: a program built from this exact graph records it, so a stale program is never replayed.

Methods

addmethod
Node add(String op, List<int> shape, DType dtype, [List<Node> src = const [], Map<String, Object?> args = const {}])
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final a = g.addInput('a', [3], DType.float32);
  final b = g.add('mul', [3], DType.float32, [a, a]);
  g.output('sq', b);
  print(CpuInterpreter(g).run({'a': Float32List.fromList([1, 2, 3])})['sq']);
}
Output
[1.0, 4.0, 9.0]
addInputmethod
Node addInput(String name, List<int> shape, DType dtype)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final n = g.addInput('pixels', [2, 4], DType.float32);
  print('node ${n.id}: ${n.op} ${n.shape} ${n.dtype}');
}
Output
node 0: input [2, 4] DType.float32
addWeightmethod
Node addWeight(String name, List<int> shape, DType dtype, Uint8List bytes)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final w = g.addWeight('w', [2], DType.float32, Float32List.fromList([0.5, 2]).buffer.asUint8List());
  g.output('w', w);
  print(CpuInterpreter(g).run({})['w']);
}
Output
[0.5, 2.0]
outputmethod
void output(String name, Node node)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.input(g, 'x', [3]);
  g.output('double', (x * 2).node);
  g.output('half', (x / 2).node);
  print(CpuInterpreter(g).run({'x': Float32List.fromList([2, 4, 6])}));
}
Output
{double: [4.0, 8.0, 12.0], half: [1.0, 2.0, 3.0]}
toBytesmethod
Uint8List toBytes()

The graph file (container STG1): what the build hook hands to tinygrad. Deterministic for a given graph.

Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.input(g, 'x', [8]);
  x.softmax().output('p');
  final b = g.toBytes();
  print('${g.nodes.length} nodes serialised to ${b.length} bytes');
}
Output
11 nodes serialised to 848 bytes
final class ProgramBuffer

A buffer of a TensorProgram: an input, an output, a weight (bytes in TensorProgram.weights) or scratch.

Constructors

ProgramBuffer.fromJson(Map<String, Object?> j)

Properties

idfield
final int id
nbytesfield
final int nbytes
dtypefield
final String dtype
kindfield
final String kind

input, output, weight or scratch.

namefield
final String? name
offsetfield
final int? offset

A weight's offset in TensorProgram.weights (256-aligned).

aliasfield
final int? alias

A scratch buffer whose lifetime doesn't overlap alias's shares its memory.

storagegetter
int get storage

The buffer whose memory this one uses (itself unless it aliases).

final class ProgramBinding

Constructors

ProgramBinding.fromJson(Map<String, Object?> j)

Properties

blockfield
final String block

The storage block name in the kernel's GLSL (B<slot>), which Evelyn GPU binds by name ('' in MSL bindings).

slotfield
final int slot

MSL bindings: the address goes at byte 8 * slot of the dispatch's root struct (-1 in GLSL bindings).

bufferfield
final int buffer
offsetfield
final int offset
nbytesfield
final int nbytes
writefield
final bool write
final class ProgramView

A byte range of a program buffer: a library step's operand or output.

Constructors

ProgramView.fromJson(Map<String, Object?> j)

Properties

bufferfield
final int buffer
offsetfield
final int offset
nbytesfield
final int nbytes
final class ProgramMslKernel

A kernel step in Riptide MSL (tinygrad's Metal renderer, simdgroup matrices included): its own launch dims, and bindings by root-struct slot.

Constructors

ProgramMslKernel.fromJson(Map<String, Object?> j)

Properties

entryfield
final String entry
globalfield
final List<int> global
localfield
final List<int> local
final List<ProgramBinding> bindings
final class ProgramStep

One step of a program: a kernel dispatch, a buffer copy, or a library call (a dense matmul / conv-as-GEMM or an attention node that tinygrad left unfused for own kernels).

Constructors

ProgramStep.fromJson(Map<String, Object?> j)

Properties

kindfield
final String kind

kernel, copy or library.

namefield
final String? name

Tinygrad's kernel name (r_16_16_64, E_32_4): what it does and its shape.

entryfield
final String? entry

The shader's name in the program's shader bundle.

globalfield
final List<int>? global

Workgroups to dispatch (x, y, z) and the invocations per workgroup the shader declares.

localfield
final List<int>? local

Workgroups to dispatch (x, y, z) and the invocations per workgroup the shader declares.

final List<ProgramBinding> bindings
srcfield
final int? src
dstfield
final int? dst
nbytesfield
final int? nbytes
mslfield
final ProgramMslKernel? msl

The same kernel in Riptide MSL (programs built with MSL; null otherwise).

opfield
final String? op

Library steps: matmul (attrs m, n, k, batch, stride_a/b/c in elements, precision) or attention (attrs batch_heads, sq, skv, head_dim, scale, causal); the graph op it came from (conv2d for conv-as-GEMM) and its node.

final String? sourceOp

Library steps: matmul (attrs m, n, k, batch, stride_a/b/c in elements, precision) or attention (attrs batch_heads, sq, skv, head_dim, scale, causal); the graph op it came from (conv2d for conv-as-GEMM) and its node.

nodefield
final int? node
attrsfield
final Map<String, Object?> attrs
inputsfield
final List<ProgramView> inputs

Library steps: operands (matmul: A, B; attention: Q, K, V) and the output, all float32 and contiguous.

outputfield
final ProgramView? output
final List<ProgramStep> fallback

Library steps: tinygrad's kernels for the same node (they write output); a backend without the library kernel runs these instead.

final class ProgramIO

Constructors

ProgramIO.fromJson(Map<String, Object?> j)

Properties

namefield
final String name
bufferfield
final int buffer
shapefield
final List<int> shape
dtypefield
final DType dtype
final class TensorProgram

A program tinygrad compiled from a TensorGraph: GLSL kernels (and, from version 2, the same kernels in Riptide MSL), the steps that dispatch them (and library steps), the buffer plan and the weights.

Constructors

factory TensorProgram.parse(Uint8List bytes)

Properties

headerfield
final Map<String, Object?> header
namefield
final String name
final String graphHash

Sha256 of the graph file this program was built from (TensorGraph.hash).

final Map<String, Object?> compiler

Tinygrad commit, renderer, BEAM width, GPU family.

buffersfield
final List<ProgramBuffer> buffers
stepsfield
final List<ProgramStep> steps
final Map<String, String> kernelSources
final Map<String, String> mslSources

The kernels in Riptide MSL by entry (empty when the program was built without MSL).

final List<Map<String, Object?>> librarySkipped

Library-op nodes the build's guard kept in tinygrad, with the reason ({node, op, shape, reason}).

inputsfield
final List<ProgramIO> inputs
outputsfield
final List<ProgramIO> outputs
weightsfield
final Uint8List weights
int get deviceBytes

Bytes of GPU memory the program allocates (aliased scratch counted once).

int get kernelCount
int get libraryCount
hasMslgetter
bool get hasMsl

Whether every kernel (fallbacks included) has an MSL version, so the program runs on Riptide.

Methods

matchesmethod
bool matches(TensorGraph graph)

Whether this program was built from exactly graph.

Uint8List weightBytes(ProgramBuffer b)
abstract interface class TensorBackend

Something that runs a TensorGraph: the CPU interpreter here, Evelyn GPU compute in riptide_tensor_gpu, and later Riptide. The Tensor API and the program file don't change when a backend is added; only this interface is implemented again.

Properties

namegetter
String get name

cpu, flutter_gpu...

Methods

runmethod
Future<Map<String, TypedData>> run(Map<String, TypedData> inputs)

Runs once: inputs by name (Float32List / Int32List / Uint32List, row-major) to outputs by name.

Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  Future<void> go() async {
    final g = TensorGraph('demo');
    final x = Tensor.input(g, 'x', [3]);
    (x * 10).output('y');
    final TensorBackend b = CpuTensorBackend(g);
    print('${b.name}: ${(await b.run({'x': Float32List.fromList([1, 2, 3])}))['y']}');
    b.dispose();
  }

  go();
}
Output
cpu: [10.0, 20.0, 30.0]
disposemethod
void dispose()
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('d');
  Tensor.input(g, 'x', [1]).output('y');
  final b = CpuTensorBackend(g);
  b.dispose();
  print('disposed ${b.name}');
}
Output
disposed cpu
final class CpuTensorBackend implements TensorBackend

The CPU backend: CpuInterpreter on the graph itself. Always available; nothing to build.

Constructors

CpuTensorBackendconstructor
CpuTensorBackend(TensorGraph graph)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('cpu');
  final x = Tensor.input(g, 'x', [4]);
  x.sigmoid().output('y');
  final b = CpuTensorBackend(g);
  b.run({'x': Float32List.fromList([-2, 0, 2, 4])}).then((r) => print('${b.name}: ${r['y']}'));
}
Output
cpu: [0.11920293420553207, 0.5, 0.8807970285415649, 0.9820137619972229]

Properties

namegetter
String get name

Methods

runmethod
Future<Map<String, TypedData>> run(Map<String, TypedData> inputs)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('cpu');
  (Tensor.input(g, 'x', [2]) + 1).output('y');
  CpuTensorBackend(g).run({'x': Float32List.fromList([1, 2])}).then((r) => print(r['y']));
}
Output
[2.0, 3.0]
disposemethod
void dispose()
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('cpu');
  Tensor.input(g, 'x', [1]).output('y');
  CpuTensorBackend(g).dispose();
  print('nothing to free on the CPU');
}
Output
nothing to free on the CPU
final class BackendChoice

Why a runtime picked the backend it did (logged by apps, asserted by tests).

Constructors

BackendChoiceconstructor
const BackendChoice(this.backend, this.reason)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('c');
  Tensor.input(g, 'x', [1]).output('y');
  final c = BackendChoice(CpuTensorBackend(g), 'no GPU program for this graph');
  print('${c.backend.name}: ${c.reason}');
}
Output
cpu: no GPU program for this graph

Properties

backendfield
final TensorBackend backend
reasonfield
final String reason
class

Tensor

final class Tensor

A lazy tensor: every op records a Node in its TensorGraph; nothing runs until the graph is run on the CPU (CpuInterpreter) or replayed as a tinygrad program on the GPU. The API mirrors tinygrad's Tensor: the core ops (elementwise, reduce, movement, matmul, conv2d) are recorded as they are, and everything else (relu, exp, softmax, cat...) is built from them the way tinygrad builds it, so tinygrad sees the same graph it would build itself.

final g = TensorGraph('mlp');
final x = Tensor.input(g, 'x', [1, 784]);
final w = Tensor.weight(g, 'w', [784, 10], weights);
g.output('y', (x.matmul(w)).relu().node);

Constructors

Tensorconstructor
Tensor(this.graph, this.node)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final n = g.addInput('x', [3], DType.float32);
  final t = Tensor(g, n);
  print('wrapped node: ${t.shape} ${t.dtype}');
  print(fmt(run(g, t * 3, {'x': Float32List.fromList([1, 2, 3])})));
}
Output
wrapped node: [3] DType.float32
3, 6, 9
factory Tensor.input(TensorGraph g, String name, List<int> shape, [DType dtype = DType.float32])
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.input(g, 'x', [2, 2]);
  print(fmt(run(g, x.transpose(), {'x': Float32List.fromList([1, 2, 3, 4])})));
}
Output
1, 3, 2, 4
factory Tensor.weight(TensorGraph g, String name, List<int> shape, TypedData data)

A constant buffer (model weights): float32 from a Float32List, int32/uint32 from Int32List/Uint32List.

Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.weight(g, 'x', [2, 3], Float32List.fromList([1, -2, 3, -4, 5, -6]));
  final v = run(g, x);
  print('x = ${fmt(v)}');
}
Output
x = 1, -2, 3, -4, 5, -6
factory Tensor.full(TensorGraph g, List<int> shape, num value, [DType dtype = DType.float32])

value in every element of shape.

Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  print(fmt(run(g, Tensor.full(g, [2, 3], 7))));
}
Output
7, 7, 7, 7, 7, 7
factory Tensor.arange(TensorGraph g, int n, [DType dtype = DType.int32])

0, 1... n - 1 (int32 or uint32). Computed in the kernels that use it; no buffer.

Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  print(fmt(run(g, Tensor.arange(g, 6))));
}
Output
0, 1, 2, 3, 4, 5

Static

broadcaststaticmethod
static (Tensor, Tensor) broadcast(Tensor a, Tensor b)

Broadcasts two tensors to one shape (numpy rules), as tinygrad's _broadcasted does, without promoting types.

Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final col = Tensor.weight(g, 'c', [3, 1], Float32List.fromList([0, 10, 20])), row = Tensor.weight(g, 'r', [1, 4], Float32List.fromList([0, 1, 2, 3]));
  final (a, b) = Tensor.broadcast(col, row);
  print('shapes ${a.shape} and ${b.shape}');
  final v = run(g, a + b);
}
Output
shapes [3, 4] and [3, 4]
catstaticmethod
static Tensor cat(List<Tensor> parts, int axis)

Concatenation as tinygrad does it: pad each part into place and add.

Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final a = Tensor.full(g, [2, 2], 1), b = Tensor.full(g, [2, 1], 2);
  final c = Tensor.cat([a, b], 1);
  final v = run(g, c);
  print('shape ${c.shape}: ${fmt(v)}');
}
Output
shape [2, 3]: 1, 1, 2, 1, 1, 2
stackstaticmethod
static Tensor stack(List<Tensor> parts, int axis)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final a = Tensor.arange(g, 3).cast(DType.float32), b = Tensor.full(g, [3], 9);
  final s = Tensor.stack([a, b], 0);
  print('shape ${s.shape}: ${fmt(run(g, s))}');
}
Output
shape [2, 3]: 0, 1, 2, 9, 9, 9

Properties

graphfield
final TensorGraph graph
nodefield
final Node node
shapegetter
List<int> get shape
dtypegetter
DType get dtype
rankgetter
int get rank
numelgetter
int get numel

Methods

outputmethod
void output(String name)

Records this tensor as the graph output name.

Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.input(g, 'x', [2]);
  (x * x).output('square');
  x.exp().output('exp');
  print(CpuInterpreter(g).run({'x': Float32List.fromList([1, 2])}));
}
Output
{square: [1.0, 4.0], exp: [2.7182817459106445, 7.3890557289123535]}
constLikemethod
Tensor constLike(num value, {List<int>? shape, DType? dtype})

A constant of this tensor's dtype (or dtype) broadcast to shape.

Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.weight(g, 'x', [2, 3], Float32List.fromList([1, -2, 3, -4, 5, -6]));
  final ones = x.constLike(1);
  print('shape ${ones.shape}, values ${fmt(run(g, ones))}');
}
Output
shape [2, 3], values 1, 1, 1, 1, 1, 1
idivmethod
Tensor idiv(Object other)

Integer division truncating toward zero (C, GLSL).

Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final a = Tensor.weight(g, 'a', [4], Int32List.fromList([7, -7, 9, 2])), b = Tensor.weight(g, 'b', [4], Int32List.fromList([2, 2, -4, 5]));
  print('idiv: ${fmt(run(g, a.idiv(b)))}');
}
Output
idiv: 3, -3, -2, 0
modmethod
Tensor mod(Object other)

Integer remainder with the dividend's sign (C, GLSL).

Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final a = Tensor.weight(g, 'a', [4], Int32List.fromList([7, -7, 9, 2])), b = Tensor.weight(g, 'b', [4], Int32List.fromList([2, 2, -4, 5]));
  print('mod: ${fmt(run(g, a.mod(b)))}');
}
Output
mod: 1, -1, 1, 2
eqmethod
Tensor eq(Object other)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final a = Tensor.weight(g, 'a', [4], Int32List.fromList([1, 2, 3, 4])), b = Tensor.weight(g, 'b', [4], Int32List.fromList([1, 0, 3, 0]));
  print('eq: ${fmt(run(g, a.eq(b)))}');
}
Output
eq: 1, 0, 1, 0
nemethod
Tensor ne(Object other)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final a = Tensor.weight(g, 'a', [4], Int32List.fromList([1, 2, 3, 4])), b = Tensor.weight(g, 'b', [4], Int32List.fromList([1, 0, 3, 0]));
  print('ne: ${fmt(run(g, a.ne(b)))}');
}
Output
ne: 0, 1, 0, 1
maximummethod
Tensor maximum(Object other)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final a = Tensor.weight(g, 'a', [4], Float32List.fromList([7, -7, 9, 2])), b = Tensor.weight(g, 'b', [4], Float32List.fromList([2, 2, -4, 5]));
  print('maximum: ${fmt(run(g, a.maximum(b)))}');
}
Output
maximum: 7, 2, 9, 5
minimummethod
Tensor minimum(Object other)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final a = Tensor.weight(g, 'a', [4], Float32List.fromList([7, -7, 9, 2])), b = Tensor.weight(g, 'b', [4], Float32List.fromList([2, 2, -4, 5]));
  print('minimum: ${fmt(run(g, a.minimum(b)))}');
}
Output
minimum: 2, -7, -4, 2
Tensor logicalNot()
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final a = Tensor.weight(g, 'a', [3], Float32List.fromList([1, 2, 3]));
  print(fmt(run(g, (a > 1.5).logicalNot())));
}
Output
1, 0, 0
Tensor reciprocal()
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final xs = [for (var i = 0; i <= 40; i++) 0.25 + (4.0 - (0.25)) * i / 40];
  final x = Tensor.weight(g, 'x', [41], Float32List.fromList(xs));
  final y = run(g, x.reciprocal());
  print('reciprocal at the ends: ${fmt([y.first, y.last])}');
}
Output
reciprocal at the ends: 4, 0.2500
sqrtmethod
Tensor sqrt()
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final xs = [for (var i = 0; i <= 40; i++) 0.0 + (4.0 - (0.0)) * i / 40];
  final x = Tensor.weight(g, 'x', [41], Float32List.fromList(xs));
  final y = run(g, x.sqrt());
  print('sqrt at the ends: ${fmt([y.first, y.last])}');
}
Output
sqrt at the ends: 0, 2
exp2method
Tensor exp2()
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final xs = [for (var i = 0; i <= 40; i++) -2.0 + (2.0 - (-2.0)) * i / 40];
  final x = Tensor.weight(g, 'x', [41], Float32List.fromList(xs));
  final y = run(g, x.exp2());
  print('exp2 at the ends: ${fmt([y.first, y.last])}');
}
Output
exp2 at the ends: 0.2500, 4
log2method
Tensor log2()
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final xs = [for (var i = 0; i <= 40; i++) 0.125 + (8.0 - (0.125)) * i / 40];
  final x = Tensor.weight(g, 'x', [41], Float32List.fromList(xs));
  final y = run(g, x.log2());
  print('log2 at the ends: ${fmt([y.first, y.last])}');
}
Output
log2 at the ends: -3, 3
sinmethod
Tensor sin()
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final xs = [for (var i = 0; i <= 40; i++) -3.2 + (3.2 - (-3.2)) * i / 40];
  final x = Tensor.weight(g, 'x', [41], Float32List.fromList(xs));
  final y = run(g, x.sin());
  print('sin at the ends: ${fmt([y.first, y.last])}');
}
Output
sin at the ends: 0.0584, -0.0584
truncmethod
Tensor trunc()
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final xs = [for (var i = 0; i <= 40; i++) -3.0 + (3.0 - (-3.0)) * i / 40];
  final x = Tensor.weight(g, 'x', [41], Float32List.fromList(xs));
  final y = run(g, x.trunc());
  print('trunc at the ends: ${fmt([y.first, y.last])}');
}
Output
trunc at the ends: -3, 3
castmethod
Tensor cast(DType to)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.weight(g, 'x', [3], Float32List.fromList([1.7, -2.5, 3.2]));
  print(fmt(run(g, x.cast(DType.int32))));
}
Output
1, -2, 3
bitcastmethod
Tensor bitcast(DType to)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.weight(g, 'x', [2], Float32List.fromList([1, -2]));
  print('float32 bits: ${run(g, x.bitcast(DType.uint32)).map((v) => v.toInt().toRadixString(16)).toList()}');
}
Output
float32 bits: [3f800000, c0000000]
wheremethod
Tensor where(Object a, Object b)

cond.where(a, b): a where this (bool) is true, else b.

Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.weight(g, 'x', [8], Float32List.fromList([for (var i = 0; i < 8; i++) -2 + i * 0.5]));
  final y = run(g, (x > 0).where(x, x * 0.1));
  print('leaky ReLU: ${fmt(y)}');
}
Output
leaky ReLU: -0.2000, -0.1500, -0.1000, -0.0500, 0, 0.5000, 1, 1.5000
expmethod
Tensor exp()
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final xs = [for (var i = 0; i <= 40; i++) -2.0 + (2.0 - (-2.0)) * i / 40];
  final x = Tensor.weight(g, 'x', [41], Float32List.fromList(xs));
  final y = run(g, x.exp());
  print('exp at the ends: ${fmt([y.first, y.last])}');
}
Output
exp at the ends: 0.1353, 7.3891
logmethod
Tensor log()
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final xs = [for (var i = 0; i <= 40; i++) 0.1 + (5.0 - (0.1)) * i / 40];
  final x = Tensor.weight(g, 'x', [41], Float32List.fromList(xs));
  final y = run(g, x.log());
  print('log at the ends: ${fmt([y.first, y.last])}');
}
Output
log at the ends: -2.3026, 1.6094
cosmethod
Tensor cos()
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final xs = [for (var i = 0; i <= 40; i++) -3.2 + (3.2 - (-3.2)) * i / 40];
  final x = Tensor.weight(g, 'x', [41], Float32List.fromList(xs));
  final y = run(g, x.cos());
  print('cos at the ends: ${fmt([y.first, y.last])}');
}
Output
cos at the ends: -0.9983, -0.9983
floormethod
Tensor floor()
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final xs = [for (var i = 0; i <= 40; i++) -3.0 + (3.0 - (-3.0)) * i / 40];
  final x = Tensor.weight(g, 'x', [41], Float32List.fromList(xs));
  final y = run(g, x.floor());
  print('floor at the ends: ${fmt([y.first, y.last])}');
}
Output
floor at the ends: -3, 3
absmethod
Tensor abs()
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final xs = [for (var i = 0; i <= 40; i++) -2.0 + (2.0 - (-2.0)) * i / 40];
  final x = Tensor.weight(g, 'x', [41], Float32List.fromList(xs));
  final y = run(g, x.abs());
  print('abs at the ends: ${fmt([y.first, y.last])}');
}
Output
abs at the ends: 2, 2
relumethod
Tensor relu()
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final xs = [for (var i = 0; i <= 40; i++) -2.0 + (2.0 - (-2.0)) * i / 40];
  final x = Tensor.weight(g, 'x', [41], Float32List.fromList(xs));
  final y = run(g, x.relu());
  print('relu at the ends: ${fmt([y.first, y.last])}');
}
Output
relu at the ends: 0, 2
clipmethod
Tensor clip(num lo, num hi)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final xs = [for (var i = 0; i <= 40; i++) -2 + i / 10];
  final y = run(g, Tensor.weight(g, 'x', [41], Float32List.fromList(xs)).clip(-1, 0.5));
  print(fmt([y.first, y.last]));
}
Output
-1, 0.5000
relu6method
Tensor relu6()
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final xs = [for (var i = 0; i <= 40; i++) -2.0 + (8.0 - (-2.0)) * i / 40];
  final x = Tensor.weight(g, 'x', [41], Float32List.fromList(xs));
  final y = run(g, x.relu6());
  print('relu6 at the ends: ${fmt([y.first, y.last])}');
}
Output
relu6 at the ends: 0, 6
sigmoidmethod
Tensor sigmoid()
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final xs = [for (var i = 0; i <= 40; i++) -6.0 + (6.0 - (-6.0)) * i / 40];
  final x = Tensor.weight(g, 'x', [41], Float32List.fromList(xs));
  final y = run(g, x.sigmoid());
  print('sigmoid at the ends: ${fmt([y.first, y.last])}');
}
Output
sigmoid at the ends: 0.0025, 0.9975
squaremethod
Tensor square()
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final xs = [for (var i = 0; i <= 40; i++) -2.0 + (2.0 - (-2.0)) * i / 40];
  final x = Tensor.weight(g, 'x', [41], Float32List.fromList(xs));
  final y = run(g, x.square());
  print('square at the ends: ${fmt([y.first, y.last])}');
}
Output
square at the ends: 4, 4
summethod
Tensor sum([List<int>? axes, bool keepdim = false])
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.weight(g, 'x', [2, 3], Float32List.fromList([1, -2, 3, -4, 5, -6]));
  print('all: ${fmt(run(g, x.sum()))}');
  final g2 = TensorGraph('b');
  final y = Tensor.weight(g2, 'x', [2, 3], Float32List.fromList([1, -2, 3, -4, 5, -6]));
  print('per row (axis 1): ${fmt(run(g2, y.sum([1])))}');
}
Output
all: -3
per row (axis 1): 2, -5
maxmethod
Tensor max([List<int>? axes, bool keepdim = false])
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.weight(g, 'x', [2, 3], Float32List.fromList([1, -2, 3, -4, 5, -6]));
  print('all: ${fmt(run(g, x.max()))}');
  final g2 = TensorGraph('b');
  final y = Tensor.weight(g2, 'x', [2, 3], Float32List.fromList([1, -2, 3, -4, 5, -6]));
  print('per row (axis 1): ${fmt(run(g2, y.max([1])))}');
}
Output
all: 5
per row (axis 1): 3, 5
meanmethod
Tensor mean([List<int>? axes, bool keepdim = false])
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.weight(g, 'x', [2, 3], Float32List.fromList([1, -2, 3, -4, 5, -6]));
  print('all: ${fmt(run(g, x.mean()))}');
  final g2 = TensorGraph('b');
  final y = Tensor.weight(g2, 'x', [2, 3], Float32List.fromList([1, -2, 3, -4, 5, -6]));
  print('per row (axis 1): ${fmt(run(g2, y.mean([1])))}');
}
Output
all: -0.5000
per row (axis 1): 0.6667, -1.6667
softmaxmethod
Tensor softmax([int axis = -1])
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.weight(g, 'x', [5], Float32List.fromList([1, 2, 3, 2, 1]));
  final p = run(g, x.softmax());
  print('${fmt(p)} (sum ${p.reduce((a, b) => a + b).toStringAsFixed(4)})');
}
Output
0.0675, 0.1834, 0.4984, 0.1834, 0.0675 (sum 1.0000)
reshapemethod
Tensor reshape(List<int> newShape)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.arange(g, 6).cast(DType.float32);
  final v = run(g, x.reshape([2, 3]));
  print(fmt(v));
}
Output
0, 1, 2, 3, 4, 5
permutemethod
Tensor permute(List<int> order)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.arange(g, 6).cast(DType.float32);
  final v = run(g, x.reshape([2, 3]).permute([1, 0]));
  print(fmt(v));
}
Output
0, 3, 1, 4, 2, 5
expandmethod
Tensor expand(List<int> newShape)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final r = Tensor.arange(g, 3).cast(DType.float32).reshape([1, 3]);
  final v = run(g, r.expand([3, 3]));
  print(fmt(v));
}
Output
0, 1, 2, 0, 1, 2, 0, 1, 2
padmethod
Tensor pad(List<(int, int)> pads)

Zero padding: pads is (before, after) per axis.

Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.full(g, [2, 2], 1);
  final v = run(g, x.pad([(1, 1), (0, 2)]));
  print('shape [4, 4]');
}
Output
shape [4, 4]
shrinkmethod
Tensor shrink(List<(int, int)> ranges)

ranges is (start, end) per axis.

Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.arange(g, 6).cast(DType.float32);
  final v = run(g, x.reshape([2, 3]).shrink([(0, 2), (1, 3)]));
  print(fmt(v));
}
Output
1, 2, 4, 5
flipmethod
Tensor flip(List<int> axes)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.arange(g, 6).cast(DType.float32);
  print(fmt(run(g, x.flip([0]))));
}
Output
5, 4, 3, 2, 1, 0
transposemethod
Tensor transpose([int a = -2, int b = -1])
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.arange(g, 6).cast(DType.float32);
  final v = run(g, x.reshape([3, 2]).transpose());
  print(fmt(v));
}
Output
0, 2, 4, 1, 3, 5
flattenmethod
Tensor flatten([int start = 0])
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.arange(g, 6).cast(DType.float32);
  final t = x.reshape([1, 2, 3]).flatten(1);
  print('shape ${t.shape}');
}
Output
shape [1, 6]
unsqueezemethod
Tensor unsqueeze(int axis)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.arange(g, 6).cast(DType.float32);
  print('${x.unsqueeze(0).shape}, ${x.unsqueeze(1).shape}');
}
Output
[1, 6], [6, 1]
slicemethod
Tensor slice(int axis, int start, int end)

Elements [start, end) along axis.

Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.arange(g, 6).cast(DType.float32);
  print(fmt(run(g, x.slice(0, 2, 5))));
}
Output
2, 3, 4
matmulmethod
Tensor matmul(Tensor other, {MatmulPrecision? precision})

[... m, k] @ [... k, n]; batch dims must match (or other is 2D). precision is the operand format a library GEMM may use for this node in library mode (default: the build's libraryPrecision).

Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final a = Tensor.weight(g, 'a', [2, 3], Float32List.fromList([1, 2, 3, 4, 5, 6])), b = Tensor.weight(g, 'b', [3, 2], Float32List.fromList([7, 8, 9, 10, 11, 12]));
  final v = run(g, a.matmul(b));
  print(fmt(v));
}
Output
58, 64, 139, 154
Tensor scaledDotProductAttention(Tensor key, Tensor value, {double? scale, bool causal = false})

softmax(scale * this @ key^T) @ value, recorded as one node (attention): this is [... Sq, d], key [... Skv, d], value [... Skv, dv], batch dims equal. scale defaults to 1 / sqrt(d). With causal, query i sees keys j \le i + (skv - sq). Tinygrad decomposes it (Q.K^T, softmax.V); a library-mode program runs it as one fused kernel where the shapes allow (head dim \frac{\frac{32}{64}}{128}, lengths multiples of 32).

Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  // 3 queries attending over 3 keys of dimension 2.
  final q = Tensor.weight(g, 'q', [3, 2], Float32List.fromList([1, 0, 0, 1, 1, 1]));
  final k = Tensor.weight(g, 'k', [3, 2], Float32List.fromList([1, 0, 0, 1, -1, 0]));
  final v = Tensor.weight(g, 'v', [3, 2], Float32List.fromList([1, 0, 0, 1, 0.5, 0.5]));
  final o = run(g, q.scaledDotProductAttention(k, v));
  print(fmt(o));
}
Output
0.6460, 0.3540, 0.3724, 0.6276, 0.5000, 0.5000
conv2dmethod
Tensor conv2d(Tensor weight, {Tensor? bias, (int, int) stride = (1, 1), ((int, int), (int, int)) padding = ((0, 0), (0, 0)), (int, int) dilation = (1, 1), int groups = 1, MatmulPrecision? precision})

NCHW input, OIHW weight (I = channels / groups). padding is ((top, bottom), (left, right)).

Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  // A 5 × 5 image with a vertical edge, convolved with a 3 × 3 edge detector (NCHW).
  final img = Tensor.weight(g, 'img', [1, 1, 5, 5], Float32List.fromList([for (var i = 0; i < 25; i++) i % 5 < 2 ? 0.0 : 1.0]));
  final k = Tensor.weight(g, 'k', [1, 1, 3, 3], Float32List.fromList([-1, 0, 1, -2, 0, 2, -1, 0, 1]));
  final v = run(g, img.conv2d(k));
  print(fmt(v));
}
Output
4, 4, 0, 4, 4, 0, 4, 4, 0
toStringmethod
String toString()
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.weight(g, 'x', [2, 3], Float32List.fromList([1, -2, 3, -4, 5, -6]));
  print(x);
  print(x.sum([1]));
}
Output
Tensor(weight#0 [2, 3] float32)
Tensor(reshape#2 [2] float32)

Operators

operator +operator
Tensor operator +(Object other)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final a = Tensor.weight(g, 'a', [4], Float32List.fromList([1, 2, 3, 4])), b = Tensor.weight(g, 'b', [4], Float32List.fromList([4, 3, 2, 1]));
  print('a + b = ${fmt(run(g, a + b))}');
  final g2 = TensorGraph('s');
  print('with a scalar: ${fmt(run(g2, Tensor.arange(g2, 4).cast(DType.float32) + 2))}');
}
Output
a + b = 5, 5, 5, 5
with a scalar: 2, 3, 4, 5
operator -operator
Tensor operator -(Object other)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final a = Tensor.weight(g, 'a', [4], Float32List.fromList([1, 2, 3, 4])), b = Tensor.weight(g, 'b', [4], Float32List.fromList([4, 3, 2, 1]));
  print('a - b = ${fmt(run(g, a - b))}');
  final g2 = TensorGraph('s');
  print('with a scalar: ${fmt(run(g2, Tensor.arange(g2, 4).cast(DType.float32) - 2))}');
}
Output
a - b = -3, -1, 1, 3
with a scalar: -2, -1, 0, 1
operator *operator
Tensor operator *(Object other)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final a = Tensor.weight(g, 'a', [4], Float32List.fromList([1, 2, 3, 4])), b = Tensor.weight(g, 'b', [4], Float32List.fromList([4, 3, 2, 1]));
  print('a * b = ${fmt(run(g, a * b))}');
  final g2 = TensorGraph('s');
  print('with a scalar: ${fmt(run(g2, Tensor.arange(g2, 4).cast(DType.float32) * 2))}');
}
Output
a * b = 4, 6, 6, 4
with a scalar: 0, 2, 4, 6
operator /operator
Tensor operator /(Object other)

True division for floats (tinygrad's x * y.reciprocal), truncating division for ints.

Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final a = Tensor.weight(g, 'a', [4], Float32List.fromList([1, 2, 3, 4])), b = Tensor.weight(g, 'b', [4], Float32List.fromList([4, 3, 2, 1]));
  print('a / b = ${fmt(run(g, a / b))}');
  final g2 = TensorGraph('s');
  print('with a scalar: ${fmt(run(g2, Tensor.arange(g2, 4).cast(DType.float32) / 2))}');
}
Output
a / b = 0.2500, 0.6667, 1.5000, 4
with a scalar: 0, 0.5000, 1, 1.5000
operator -operator
Tensor operator -()
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  print(fmt(run(g, -Tensor.arange(g, 4).cast(DType.float32))));
}
Output
-0, -1, -2, -3
operator &operator
Tensor operator &(Object other)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final a = Tensor.weight(g, 'a', [4], Int32List.fromList([0xAA, 0x55, 0x0F, 0xF0]));
  print('a & b: ${run(g, a & Tensor.weight(g, 'b', [4], Int32List.fromList([0xF0, 0x0F, 0xFF, 0x00]))).map((v) => v.toInt().toRadixString(16)).toList()}');
}
Output
a & b: [a0, 5, f, 0]
operator |operator
Tensor operator |(Object other)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final a = Tensor.weight(g, 'a', [4], Int32List.fromList([0xAA, 0x55, 0x0F, 0xF0]));
  print('a | b: ${run(g, a | Tensor.weight(g, 'b', [4], Int32List.fromList([0xF0, 0x0F, 0xFF, 0x00]))).map((v) => v.toInt().toRadixString(16)).toList()}');
}
Output
a | b: [fa, 5f, ff, f0]
operator ^operator
Tensor operator ^(Object other)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final a = Tensor.weight(g, 'a', [4], Int32List.fromList([0xAA, 0x55, 0x0F, 0xF0]));
  print('a ^ b: ${run(g, a ^ Tensor.weight(g, 'b', [4], Int32List.fromList([0xF0, 0x0F, 0xFF, 0x00]))).map((v) => v.toInt().toRadixString(16)).toList()}');
}
Output
a ^ b: [5a, 5a, f0, f0]
operator <<operator
Tensor operator <<(Object other)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final a = Tensor.weight(g, 'a', [4], Int32List.fromList([0xAA, 0x55, 0x0F, 0xF0]));
  print('a << 2: ${run(g, a << 2).map((v) => v.toInt().toRadixString(16)).toList()}');
}
Output
a << 2: [2a8, 154, 3c, 3c0]
operator >>operator
Tensor operator >>(Object other)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final a = Tensor.weight(g, 'a', [4], Int32List.fromList([0xAA, 0x55, 0x0F, 0xF0]));
  print('a >> 2: ${run(g, a >> 2).map((v) => v.toInt().toRadixString(16)).toList()}');
}
Output
a >> 2: [2a, 15, 3, 3c]
operator <operator
Tensor operator <(Object other)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.weight(g, 'x', [8], Float32List.fromList([for (var i = 0; i < 8; i++) -2 + i * 0.5]));
  print('x < 0: ${fmt(run(g, x < 0))}');
}
Output
x < 0: 1, 1, 1, 1, 0, 0, 0, 0
operator >operator
Tensor operator >(Object other)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.weight(g, 'x', [8], Float32List.fromList([for (var i = 0; i < 8; i++) -2 + i * 0.5]));
  print('x > 0: ${fmt(run(g, x > 0))}');
}
Output
x > 0: 0, 0, 0, 0, 0, 1, 1, 1
operator <=operator
Tensor operator <=(Object other)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.weight(g, 'x', [8], Float32List.fromList([for (var i = 0; i < 8; i++) -2 + i * 0.5]));
  print('x <= 0: ${fmt(run(g, x <= 0))}');
}
Output
x <= 0: 1, 1, 1, 1, 1, 0, 0, 0
operator >=operator
Tensor operator >=(Object other)
Example
import 'package:riptide_tensor/riptide_tensor.ray';
import 'raya:convert';
import 'raya:math' as math;
import 'raya:typed_data';

// Runs [t] (an output of [g]) on the CPU interpreter with [inputs]; returns its values as doubles.
List<double> run(TensorGraph g, Tensor t, [Map<String, TypedData> inputs = const {}]) {
  t.output('out');
  final d = CpuInterpreter(g).run(inputs)['out']!;
  return [for (final v in (d as List)) (v as num).toDouble()];
}

String fmt(List<double> v) => v.map((x) => x == x.roundToDouble() ? x.toStringAsFixed(0) : x.toStringAsFixed(4)).join(', ');

void main() {
  final g = TensorGraph('demo');
  final x = Tensor.weight(g, 'x', [8], Float32List.fromList([for (var i = 0; i < 8; i++) -2 + i * 0.5]));
  print('x >= 0: ${fmt(run(g, x >= 0))}');
}
Output
x >= 0: 0, 0, 0, 0, 1, 1, 1, 1
final class ProgramOptions

The options a ProgramCompiler builds a program with (buildTensorPrograms' tuning arguments).

Constructors

ProgramOptionsconstructor
const ProgramOptions({this.beam = 0, this.tuneDevice, this.family = 'generic', this.library = const {}, this.libraryPrecision = MatmulPrecision.f16, this.libraryMinFlops = 1 << 20, this.msl = true})

Properties

beamfield
final int beam
final String? tuneDevice
familyfield
final String family
libraryfield
final Set<String> library
final MatmulPrecision libraryPrecision
final int libraryMinFlops
mslfield
final bool msl
abstract interface class ProgramCompiler

Turns a recorded graph into a program file (STP1): the Riptide compiler (package:riptide_compiler, in process; the default through its hook, tinygrad building the graphs it can't) or TinygradToolchain (RIPTIDE_COMPILER=tinygrad on that hook). This package's own hook always builds with tinygrad: it can't import riptide_compiler. The program format and the runtimes are the same for every compiler.

Properties

namegetter
String get name

tinygrad or riptide.

commitgetter
String get commit

The compiler's version (and its sources' hash): part of every built program's cache key.

impellercgetter
String? get impellerc

Methods

Future<void> compileProgram(String name, TensorGraph graph, Directory outDir, File program, ProgramOptions options)

Writes the program for graph to program. Throws ProgramCompileException or ProcessException when it can't.

final class ProgramCompileException implements Exception

A ProgramCompiler could not build a graph.

Constructors

const ProgramCompileException(this.message)

Properties

messagefield
final String message

Methods

toStringmethod
String toString()
final class TinygradToolchain implements ProgramCompiler

Constructors

const TinygradToolchain({required this.python, required this.checkout, required this.commit, this.package, this.impellerc})

Static

locatestaticmethod
static TinygradToolchain? locate({required Directory from, Map<String, String>? environment})

Null when tinygrad isn't set up.

Properties

namegetter
String get name
pythonfield
final String python

The venv's python.

final String checkout
commitfield
final String commit

The tinygrad commit plus a hash of package's sources: the cache key for built programs.

packagefield
final String? package
final String? impellerc

Methods

Future<void> compileProgram(String name, TensorGraph graph, Directory outDir, File program, ProgramOptions options)

Runs tinygrad on graph and writes the program to program.

final class TensorProgramBuild

What buildTensorPrograms did for one graph.

Constructors

const TensorProgramBuild(this.name, this.status, this.reason, {this.program, this.shaderBundle})

Properties

namefield
final String name
statusfield
final String status

Skipped and failed graphs run on the CPU.

reasonfield
final String reason
programfield
final File? program
final File? shaderBundle

Methods

toStringmethod
String toString()