Tensor Language¶
anvil functions are written using tinygrad's Tensor API. The anvil.language module re-exports everything you need and applies anvil-specific defaults.
Imports¶
Always import tensor types from anvil or anvil.language, not from tinygrad directly:
This ensures consistent defaults across the codebase.
Defaults¶
anvil sets two important defaults that differ from tinygrad:
- Default float type:
float64(tinygrad's default isfloat32) - Default device:
CPU
These are set at import time by anvil.language.
Methods, not functions¶
Unlike numpy or PyTorch, tinygrad exposes all mathematical operations as methods on Tensor, not as standalone functions. There is no av.sin(x) -- instead, write x.sin():
# numpy/PyTorch style (does NOT work):
# result = np.sin(x) + np.exp(y)
# tinygrad/anvil style:
result = x.sin() + y.exp()
This applies to all elementwise math: trigonometric, exponential, logarithmic, etc.
Supported operations¶
Inside a NumericalFunction body, you can use the full tinygrad Tensor API. The operations that anvil's code generation and automatic differentiation support include:
Elementwise¶
| Operation | Example |
|---|---|
| Negation | -x |
| Addition | x + y |
| Subtraction | x - y |
| Multiplication | x * y |
| Division | x / y |
| Power | x ** 2, x.pow(y) |
| Square | x.square() |
| Square root | x.sqrt() |
| Reciprocal | x.reciprocal() |
| Exponential | x.exp(), x.exp2() |
| Logarithm | x.log(), x.log2() |
| Trigonometric | x.sin(), x.cos(), x.tan() |
| Comparison | x < y, x == y, x != y |
| Conditional | x.where(y, z) |
| Clamp | x.clamp(min, max) |
| Absolute value | x.abs() |
| Cast | x.cast(av.dtypes.float32) |
Reductions¶
| Operation | Example |
|---|---|
| Sum | x.sum(), x.sum(axis=0) |
| Max | x.max(), x.max(axis=1) |
Shape manipulation¶
| Operation | Example |
|---|---|
| Reshape | x.reshape(3, 4) |
| Permute | x.permute(1, 0) |
| Expand | x.expand(3, 4) |
| Pad | x.pad(((1, 1),)) |
| Shrink (slice) | x[2:5], x[:, 1:3] |
| Flip | x.flip(0) |
| Concatenate | av.Tensor.cat(x, y, dim=0) |
| Stack | av.Tensor.stack(x, y) |
Linear algebra¶
| Operation | Example |
|---|---|
| Matrix multiply | A @ x, x.matmul(y) |
| Dot product | (x * y).sum() |
Constructors¶
| Operation | Example |
|---|---|
| From data | av.Tensor([1.0, 2.0, 3.0]) |
| Zeros | av.Tensor.zeros(3) |
| Ones | av.Tensor.ones(3) |
| Full | av.Tensor.full((3,), 2.0) |
| Eye | av.Tensor.eye(3) |
Tip
For the full tinygrad Tensor API, see the tinygrad documentation. Most operations that tinygrad supports can be used in anvil function bodies. The AD system supports a broad subset -- elementwise, reductions, and movement operations are fully covered.