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, plusscaledDotProductAttentionas oneattentionnode; everything else (relu, exp, softmax, cat...) is built from them the way tinygrad builds it.matmul/conv2dtake an optionalMatmulPrecisionfor 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:buildTensorProgramsHookfor a package'shook/build.ray.ProgramCompiler: what turns a graph into a program.TinygradToolchainis the default; the Riptide compiler implements it in process and is picked withRIPTIDE_COMPILER=riptide|boththroughpackage: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 = 30The 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
TensorContainer
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
static const graphMagic = 'STG1'static const programMagic = 'STP1'static const version = 1static Uint8List pack(String magic, Map<String, Object?> header, [Uint8List? blob])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))}');
}52 bytes, magic DEMO
static (Map<String, Object?>, Uint8List) unpack(Uint8List data, String magic)The header and the blob (a view into data, no copy).
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');
}header {answer: 42}, blob [9, 8, 7]CpuInterpreter
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
CpuInterpreter(this.graph)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']}');
}y = [2.0, 5.0, 10.0]
Static
static TypedData alloc(DType dtype, int n)Allocates the typed list for n elements of dtype.
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));
}false [0, 0]
Properties
final TensorGraph graphMethods
Map<String, TypedData> run(Map<String, TypedData> inputs)Runs the graph. inputs are typed lists (Float32List for float32, Int32List, Uint32List) in row-major order.
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']}');
}total [9.0], largest [4.0]
DType
enum DType
Element types. GPU programs store every type in 32 bits; bool exists inside kernels and on the CPU only.
Values
float32('float32')int32('int32')uint32('uint32')bool_('bool')Constructors
const DType(this.wire)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}');
}
}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
static DType fromWire(String name)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'));
}DType.int32 DType.bool_
Properties
bool get isFloatbool get isIntNode
final class Node
One recorded op. Ops and their arguments mirror tinygrad; CpuInterpreter defines what each computes.
Properties
final int idfinal String opfinal List<int> shapefinal DType dtypefinal List<Node> srcfinal Map<String, Object?> argsint get numelMethods
Map<String, Object?> toJson()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());
}{id: 2, op: mul, shape: [2], dtype: float32, src: [0, 1]}Ops
abstract final class Ops
Ops a TensorGraph records.
Static
static const leaves = {'input', 'weight', 'const', 'arange'}static const unary = {'neg', 'recip', 'sqrt', 'exp2', 'log2', 'sin', 'trunc'}static const binarystatic const compare = {'cmplt', 'cmpne', 'cmpeq'}static const reduce = {'sum', 'rmax'}static const movement = {'reshape', 'permute', 'expand', 'pad', 'shrink', 'flip'}static const other = {'cast', 'bitcast', 'where', 'matmul', 'conv2d', 'attention'}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.
MatmulPrecision
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
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
const MatmulPrecision(this.wire)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}');
}
}f32: wire f32 f16: wire f16 bf16: wire bf16 f32x3: wire f32x3
Properties
final String wireTensorGraph
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
TensorGraph(this.name)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');
}graph "demo" has 5 nodes
Properties
final String namefinal List<Node> nodesfinal Map<String, Node> outputsfinal Map<String, Node> inputsfinal Map<String, Uint8List> weightsString get hashSha256 of toBytes: a program built from this exact graph records it, so a stale program is never replayed.
Methods
Node add(String op, List<int> shape, DType dtype, [List<Node> src = const [], Map<String, Object?> args = const {}])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']);
}[1.0, 4.0, 9.0]
Node addInput(String name, List<int> shape, DType dtype)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}');
}node 0: input [2, 4] DType.float32
Node addWeight(String name, List<int> shape, DType dtype, Uint8List bytes)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']);
}[0.5, 2.0]
void output(String name, Node node)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])}));
}{double: [4.0, 8.0, 12.0], half: [1.0, 2.0, 3.0]}Uint8List toBytes()The graph file (container STG1): what the build hook hands to tinygrad. Deterministic for a given graph.
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');
}11 nodes serialised to 848 bytes
ProgramBuffer
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
final int idfinal int nbytesfinal String dtypefinal String? nameProgramBinding
final class ProgramBinding
Constructors
ProgramBinding.fromJson(Map<String, Object?> j)Properties
final String blockThe storage block name in the kernel's GLSL (B<slot>), which Evelyn GPU binds by name ('' in MSL bindings).
final int slotMSL bindings: the address goes at byte 8 * slot of the dispatch's root struct (-1 in GLSL bindings).
final int bufferfinal int offsetfinal int nbytesfinal bool writeProgramView
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
final int bufferfinal int offsetfinal int nbytesProgramMslKernel
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
final String entryfinal List<int> globalfinal List<int> localfinal List<ProgramBinding> bindingsProgramStep
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
final List<int>? globalWorkgroups to dispatch (x, y, z) and the invocations per workgroup the shader declares.
final List<int>? localWorkgroups to dispatch (x, y, z) and the invocations per workgroup the shader declares.
final List<ProgramBinding> bindingsfinal int? srcfinal int? dstfinal int? nbytesfinal ProgramMslKernel? mslThe same kernel in Riptide MSL (programs built with MSL; null otherwise).
final String? opLibrary 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? sourceOpLibrary 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 int? nodefinal Map<String, Object?> attrsfinal List<ProgramView> inputsLibrary steps: operands (matmul: A, B; attention: Q, K, V) and the output, all float32 and contiguous.
final ProgramView? outputProgramIO
final class ProgramIO
Constructors
ProgramIO.fromJson(Map<String, Object?> j)Properties
final String namefinal int bufferfinal List<int> shapefinal DType dtypeTensorProgram
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
final Map<String, Object?> headerfinal String namefinal String graphHashSha256 of the graph file this program was built from (TensorGraph.hash).
final List<ProgramBuffer> buffersfinal List<ProgramStep> stepsfinal Map<String, String> kernelSourcesfinal Map<String, String> mslSourcesThe kernels in Riptide MSL by entry (empty when the program was built without MSL).
final List<Map<String, Object?>> librarySkippedLibrary-op nodes the build's guard kept in tinygrad, with the reason ({node, op, shape, reason}).
final List<ProgramIO> inputsfinal List<ProgramIO> outputsfinal Uint8List weightsint get deviceBytesBytes of GPU memory the program allocates (aliased scratch counted once).
int get kernelCountint get libraryCountbool get hasMslWhether every kernel (fallbacks included) has an MSL version, so the program runs on Riptide.
Methods
Uint8List weightBytes(ProgramBuffer b)TensorBackend
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
Methods
Future<Map<String, TypedData>> run(Map<String, TypedData> inputs)Runs once: inputs by name (Float32List / Int32List / Uint32List, row-major) to outputs by name.
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();
}cpu: [10.0, 20.0, 30.0]
void dispose()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}');
}disposed cpu
CpuTensorBackend
final class CpuTensorBackend implements TensorBackend
The CPU backend: CpuInterpreter on the graph itself. Always available; nothing to build.
Constructors
CpuTensorBackend(TensorGraph graph)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']}'));
}cpu: [0.11920293420553207, 0.5, 0.8807970285415649, 0.9820137619972229]
Properties
String get nameMethods
Future<Map<String, TypedData>> run(Map<String, TypedData> inputs)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']));
}[2.0, 3.0]
void dispose()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');
}nothing to free on the CPU
BackendChoice
final class BackendChoice
Why a runtime picked the backend it did (logged by apps, asserted by tests).
Constructors
const BackendChoice(this.backend, this.reason)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}');
}cpu: no GPU program for this graph
Properties
final TensorBackend backendfinal String reasonTensor
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
Tensor(this.graph, this.node)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])})));
}wrapped node: [3] DType.float32 3, 6, 9
factory Tensor.input(TensorGraph g, String name, List<int> shape, [DType dtype = DType.float32])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])})));
}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.
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)}');
}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.
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))));
}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.
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))));
}0, 1, 2, 3, 4, 5
Static
static (Tensor, Tensor) broadcast(Tensor a, Tensor b)Broadcasts two tensors to one shape (numpy rules), as tinygrad's _broadcasted does, without promoting types.
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);
}shapes [3, 4] and [3, 4]
static Tensor cat(List<Tensor> parts, int axis)Concatenation as tinygrad does it: pad each part into place and add.
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)}');
}shape [2, 3]: 1, 1, 2, 1, 1, 2
static Tensor stack(List<Tensor> parts, int axis)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))}');
}shape [2, 3]: 0, 1, 2, 9, 9, 9
Properties
final TensorGraph graphfinal Node nodeList<int> get shapeDType get dtypeint get rankint get numelMethods
void output(String name)Records this tensor as the graph output name.
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])}));
}{square: [1.0, 4.0], exp: [2.7182817459106445, 7.3890557289123535]}Tensor constLike(num value, {List<int>? shape, DType? dtype})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))}');
}shape [2, 3], values 1, 1, 1, 1, 1, 1
Tensor idiv(Object other)Integer division truncating toward zero (C, GLSL).
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)))}');
}idiv: 3, -3, -2, 0
Tensor mod(Object other)Integer remainder with the dividend's sign (C, GLSL).
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)))}');
}mod: 1, -1, 1, 2
Tensor eq(Object other)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)))}');
}eq: 1, 0, 1, 0
Tensor ne(Object other)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)))}');
}ne: 0, 1, 0, 1
Tensor maximum(Object other)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)))}');
}maximum: 7, 2, 9, 5
Tensor minimum(Object other)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)))}');
}minimum: 2, -7, -4, 2
Tensor logicalNot()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())));
}1, 0, 0
Tensor reciprocal()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])}');
}reciprocal at the ends: 4, 0.2500
Tensor sqrt()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])}');
}sqrt at the ends: 0, 2
Tensor exp2()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])}');
}exp2 at the ends: 0.2500, 4
Tensor log2()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])}');
}log2 at the ends: -3, 3
Tensor sin()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])}');
}sin at the ends: 0.0584, -0.0584
Tensor trunc()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])}');
}trunc at the ends: -3, 3
Tensor cast(DType to)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))));
}1, -2, 3
Tensor bitcast(DType to)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()}');
}float32 bits: [3f800000, c0000000]
Tensor where(Object a, Object b)cond.where(a, b): a where this (bool) is true, else b.
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)}');
}leaky ReLU: -0.2000, -0.1500, -0.1000, -0.0500, 0, 0.5000, 1, 1.5000
Tensor exp()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])}');
}exp at the ends: 0.1353, 7.3891
Tensor log()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])}');
}log at the ends: -2.3026, 1.6094
Tensor cos()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])}');
}cos at the ends: -0.9983, -0.9983
Tensor floor()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])}');
}floor at the ends: -3, 3
Tensor abs()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])}');
}abs at the ends: 2, 2
Tensor relu()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])}');
}relu at the ends: 0, 2
Tensor clip(num lo, num hi)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]));
}-1, 0.5000
Tensor relu6()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])}');
}relu6 at the ends: 0, 6
Tensor sigmoid()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])}');
}sigmoid at the ends: 0.0025, 0.9975
Tensor square()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])}');
}square at the ends: 4, 4
Tensor sum([List<int>? axes, bool keepdim = false])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])))}');
}all: -3 per row (axis 1): 2, -5
Tensor max([List<int>? axes, bool keepdim = false])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])))}');
}all: 5 per row (axis 1): 3, 5
Tensor mean([List<int>? axes, bool keepdim = false])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])))}');
}all: -0.5000 per row (axis 1): 0.6667, -1.6667
Tensor softmax([int axis = -1])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)})');
}0.0675, 0.1834, 0.4984, 0.1834, 0.0675 (sum 1.0000)
Tensor reshape(List<int> newShape)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));
}0, 1, 2, 3, 4, 5
Tensor permute(List<int> order)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));
}0, 3, 1, 4, 2, 5
Tensor expand(List<int> newShape)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));
}0, 1, 2, 0, 1, 2, 0, 1, 2
Tensor pad(List<(int, int)> pads)Zero padding: pads is (before, after) per axis.
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]');
}shape [4, 4]
Tensor shrink(List<(int, int)> ranges)ranges is (start, end) per axis.
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));
}1, 2, 4, 5
Tensor flip(List<int> axes)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]))));
}5, 4, 3, 2, 1, 0
Tensor transpose([int a = -2, int b = -1])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));
}0, 2, 4, 1, 3, 5
Tensor flatten([int start = 0])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}');
}shape [1, 6]
Tensor unsqueeze(int axis)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}');
}[1, 6], [6, 1]
Tensor slice(int axis, int start, int end)Elements [start, end) along axis.
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))));
}2, 3, 4
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).
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));
}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).
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));
}0.6460, 0.3540, 0.3724, 0.6276, 0.5000, 0.5000
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)).
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));
}4, 4, 0, 4, 4, 0, 4, 4, 0
String toString()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]));
}Tensor(weight#0 [2, 3] float32) Tensor(reshape#2 [2] float32)
Operators
Tensor operator +(Object other)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))}');
}a + b = 5, 5, 5, 5 with a scalar: 2, 3, 4, 5
Tensor operator -(Object other)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))}');
}a - b = -3, -1, 1, 3 with a scalar: -2, -1, 0, 1
Tensor operator *(Object other)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))}');
}a * b = 4, 6, 6, 4 with a scalar: 0, 2, 4, 6
Tensor operator /(Object other)True division for floats (tinygrad's x * y.reciprocal), truncating division for ints.
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))}');
}a / b = 0.2500, 0.6667, 1.5000, 4 with a scalar: 0, 0.5000, 1, 1.5000
Tensor operator -()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))));
}-0, -1, -2, -3
Tensor operator &(Object other)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()}');
}a & b: [a0, 5, f, 0]
Tensor operator |(Object other)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()}');
}a | b: [fa, 5f, ff, f0]
Tensor operator ^(Object other)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()}');
}a ^ b: [5a, 5a, f0, f0]
Tensor operator <<(Object other)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()}');
}a << 2: [2a8, 154, 3c, 3c0]
Tensor operator >>(Object other)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()}');
}a >> 2: [2a, 15, 3, 3c]
Tensor operator <(Object other)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))}');
}x < 0: 1, 1, 1, 1, 0, 0, 0, 0
Tensor operator >(Object other)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))}');
}x > 0: 0, 0, 0, 0, 0, 1, 1, 1
Tensor operator <=(Object other)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))}');
}x <= 0: 1, 1, 1, 1, 1, 0, 0, 0
Tensor operator >=(Object other)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))}');
}x >= 0: 0, 0, 0, 0, 1, 1, 1, 1
ProgramOptions
final class ProgramOptions
The options a ProgramCompiler builds a program with (buildTensorPrograms' tuning arguments).
Constructors
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
final int beamfinal String? tuneDevicefinal String familyfinal Set<String> libraryfinal MatmulPrecision libraryPrecisionfinal int libraryMinFlopsfinal bool mslProgramCompiler
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
String get commitThe compiler's version (and its sources' hash): part of every built program's cache key.
String? get impellercMethods
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
final String messageMethods
String toString()TinygradToolchain
final class TinygradToolchain implements ProgramCompiler
Constructors
const TinygradToolchain({required this.python, required this.checkout, required this.commit, this.package, this.impellerc})Static
static TinygradToolchain? locate({required Directory from, Map<String, String>? environment})Null when tinygrad isn't set up.
Properties
String get namefinal String checkoutfinal String commitThe tinygrad commit plus a hash of package's sources: the cache key for built programs.
final String? packagefinal String? impellercMethods
Future<void> compileProgram(String name, TensorGraph graph, Directory outDir, File program, ProgramOptions options)Runs tinygrad on graph and writes the program to program.
TensorProgramBuild
final class TensorProgramBuild
What buildTensorPrograms did for one graph.
Constructors
const TensorProgramBuild(this.name, this.status, this.reason, {this.program, this.shaderBundle})Properties
final String namefinal String reasonfinal File? programfinal File? shaderBundleMethods
String toString()