Data Types
May 27, 2026 · View on GitHub
Data Types
Magnetron currently supports the following data types, with additional formats planned (e.g. MXFP8).
| DType | Type | Size (bits) | Min value | Max value |
|---|---|---|---|---|
| float16 | Floating point | 16 | ||
| bfloat16 | Floating point | 16 | ||
| float32 | Floating point | 32 | ||
| boolean | Boolean | 8 | $0$ | $1$ |
| int8 | Integer (signed) | 8 | $2^{7}-1$ | |
| uint8 | Integer (unsigned) | 8 | $0$ | $2^{8}-1$ |
| int16 | Integer (signed) | 16 | $2^{15}-1$ | |
| uint16 | Integer (unsigned) | 16 | $0$ | $2^{16}-1$ |
| int32 | Integer (signed) | 32 | $2^{31}-1$ | |
| uint32 | Integer (unsigned) | 32 | $0$ | $2^{32}-1$ |
| int64 | Integer (signed) | 64 | $2^{63}-1$ |
Examples
Create a float16 tensor filled with zeros:
Tensor.zeros(10, dtype=dtype.float16)
``$
\text{Create} \text{a} 2 \times 3 $float32` tensor filled with ones:
```python
Tensor.ones(2, 3, dtype=dtype.float32)
Create a range tensor of integers from 0 to 9:
Tensor.arange(0, 10, dtype=dtype.int64)
Operators
All operations in Magnetron are exposed as methods on Tensor.
If you are familiar with PyTorch, think x.sin() instead of torch.sin(x).
Tensor Creation & Initialization
| Method | Description | Math | Example |
|---|---|---|---|
Tensor.empty(shape) | Create an uninitialized tensor (contents undefined) | N/A | x = Tensor.empty(2, 3) |
Tensor.empty_like(x) | Create an uninitialized tensor with same shape and dtype as input | N/A | x = Tensor.empty_like(x) |
Tensor.full(shape, v) | Create tensor filled with constant value | x = Tensor.full(2, 3, fill_value=3.0) | |
Tensor.full_like(x, v) | Create constant-filled tensor with the same shape as input | x = Tensor.full_like(x, fill_value=3.0) | |
Tensor.zeros(shape) | Create tensor filled with zeros | x = Tensor.zeros(2, 3) | |
Tensor.zeros_like(x) | Create tensor filled with zeros and same shape as input | x = Tensor.zeros_like(x) | |
Tensor.ones(shape) | Create tensor filled with ones | x = Tensor.ones(2, 3) | |
Tensor.ones_like(x) | Create tensor filled with ones and same shape as input | x = Tensor.ones_like(x) | |
Tensor.uniform(a,b) | Fill with uniform random values | x = Tensor.uniform(2, 3, low=-1.0, high=1.0) | |
Tensor.uniform_like(x, a, b) | Create tensor filled with uniform random values and same shape as input | x = Tensor.uniform_like(x, low=-1.0, high=1.0) | |
Tensor.normal(μ, σ) | Fill with normal distribution | x = Tensor.normal(2, 3, mean=0.0, std=1.0) | |
Tensor.normal_like(x, μ, σ) | Create tensor filled with normal random values and same shape as input | x = Tensor.normal_like(x, mean=0.0, std=1.0) | |
Tensor.bernoulli(p) | Fill with Bernoulli samples | x = Tensor.bernoulli(2, 3, p=0.5) | |
Tensor.bernoulli_like(x, p) | Create tensor filled with bernoulli random values and same shape as input | x = Tensor.bernoulli_like(x, p=0.5) | |
Tensor.arange(a,b,s) | Create evenly spaced values from start to stop | x = Tensor.arange(0, 10, 2) | |
Tensor.rand_perm(n) | Random permutation of integers | permutation of | x = Tensor.rand_perm(10) |
Tensor.one_hot(c) | Convert indices to one-hot vectors | x = idx.one_hot(10) | |
Tensor.load_image(p) | Load image file into tensor | N/A | img = Tensor.load_image("img.png") |
Type & Device Conversion
| Method | Description | Example |
|---|---|---|
cast(dtype) | Return tensor with new dtype | x.cast(dtype.float16) |
transfer(device) | Move tensor to device | x.transfer('cuda') |
Filling & Mutation
| Method | Description | Math | Example |
|---|---|---|---|
fill_(v) | Fill tensor in-place with constant value | x.fill_(0) | |
zeros_() | In-place fill with zeros | x.zeros_() | |
ones_() | In-place fill with ones | x.ones_() | |
copy_(y) | Copy data from another tensor | x.copy_(y) | |
masked_fill(mask,v) | Replace values where mask is true | if | y = x.masked_fill(m,0) |
masked_fill_(mask,v) | In-place masked fill | N/A | x.masked_fill_(m,0) |
uniform_(a,b) | Fill with uniform random values | x.uniform_(0,1) | |
normal_(μ,σ) | Fill with normal distribution | x.normal_(0,1) | |
bernoulli_(p) | Fill with Bernoulli samples | x.bernoulli_(0.5) | |
Tensor.where(cond,x,y) | Conditional elementwise selection | z = Tensor.where(x > 0, x, 0) | |
clamp(min,max) | Clamp values into an interval | y = x.clamp(-1, 1) |
Shape, Views & Indexing
| Method | Description | Math | Example |
|---|---|---|---|
clone() | Create a deep copy of the tensor | N/A | y = x.clone() |
copy_(src) | Copy data from src into this tensor in-place | x.copy_(y) | |
view(shape) | Create a view with new shape | N/A | x.view(2, -1) |
view_slice(d,s,l) | Slice tensor without copying | x.view_slice(0,0,4) | |
reshape(shape) | Reshape tensor (may copy) | N/A | x.reshape(4,3) |
transpose(a,b) | Swap two dimensions | x.transpose(0,1) | |
permute(dims) | Reorder dimensions arbitrarily | N/A | x.permute(1,0,2) |
contiguous() | Ensure tensor is contiguous in memory | N/A | x = x.contiguous() |
squeeze() | Remove dimensions of size 1 | N/A | x.squeeze() |
unsqueeze(d) | Insert new dimension | N/A | x.unsqueeze(0) |
flatten(a,b) | Flatten a range of dimensions | N/A | x.flatten(1) |
unflatten(s) | Expand flattened dimension | N/A | x.unflatten((2,3)) |
narrow(d,s,l) | Take slice along dimension | N/A | x.narrow(1,0,4) |
movedim(a,b) | Move dimension to new position | N/A | x.movedim(0,-1) |
select(d,i) | Select single index along dim | x.select(1,2) | |
split(n,d) | Split tensor into chunks | N/A | x.split(4,1) |
cat(xs,d) | Concatenate tensors | N/A | Tensor.cat([a,b],1) |
gather(d,idx) | Gather elements by index tensor | x.gather(1,idx) |
Reductions
| Method | Description | Math | Example |
|---|---|---|---|
mean(dim=-1) | Compute mean over elements | x.mean() | |
sum(dim=-1) | Sum elements | x.sum() | |
prod(dim=-1) | Product of elements | x.prod() | |
min(dim=None, keepdim=False) | Reduction minimum, or elementwise min if argument is tensor/scalar | / | x.min() / x.min(y) |
max(dim=None, keepdim=False) | Reduction maximum, or elementwise max if argument is tensor/scalar | / | x.max() / x.max(y) |
argmin(dim=-1) | Index of minimum | x.argmin() | |
argmax(dim=-1) | Index of maximum | x.argmax() | |
any(dim=-1) | True if any element is non-zero | x.any() | |
all(dim=-1) | True if all elements are non-zero | x.all() | |
topk(k, dim=-1, largest=True, sorted=True) | Select k largest values | values, indices = x.topk(k) |
Unary Math Operations
| Method | Description | Math (per element) | Example |
|---|---|---|---|
abs() | Absolute value | y = x.abs() | |
sgn() | Sign of each element | y = x.sgn() | |
neg() | Negation | y = x.neg() | |
sqr() | Square | y = x.sqr() | |
rcp() | Reciprocal | y = x.rcp() | |
sqrt() | Square root | y = x.sqrt() | |
rsqrt() | Inverse square root | y = x.rsqrt() | |
log() | Natural logarithm | y = x.log() | |
log2() | Base-2 logarithm | y = x.log2() | |
log10() | Base-10 logarithm | y = x.log10() | |
log1p() | Log of (1 + x) | y = x.log1p() | |
exp() | Exponential | y = x.exp() | |
exp2() | Power of 2 | y = x.exp2() | |
expm1() | Exponential minus 1 | y = x.expm1() | |
floor() | Round down | y = x.floor() | |
ceil() | Round up | y = x.ceil() | |
round() | Round to nearest integer | y = x.round() | |
trunc() | Truncate fractional part | y = x.trunc() | |
sin() | Sine | y = x.sin() | |
cos() | Cosine | y = x.cos() | |
tan() | Tangent | y = x.tan() | |
asin() | Inverse sine | y = x.asin() | |
acos() | Inverse cosine | y = x.acos() | |
atan() | Inverse tangent | y = x.atan() | |
sinh() | Hyperbolic sine | y = x.sinh() | |
cosh() | Hyperbolic cosine | y = x.cosh() | |
tanh() | Hyperbolic tangent | y = x.tanh() | |
asinh() | Inverse hyperbolic sine | y = x.asinh() | |
acosh() | Inverse hyperbolic cosine | y = x.acosh() | |
atanh() | Inverse hyperbolic tangent | y = x.atanh() | |
step() | Heaviside step (0/1) | y = x.step() | |
erf() | Error function | y = x.erf() | |
erfc() | Complementary error function | y = x.erfc() | |
softmax() | Softmax over last dim | y = x.softmax() | |
sigmoid() | Logistic sigmoid | y = x.sigmoid() | |
hard_sigmoid() | Piecewise linear sigmoid | y = x.hard_sigmoid() | |
silu() | SiLU / Swish | y = x.silu() | |
relu() | Rectified Linear Unit | y = x.relu() | |
gelu() | Exact GELU | y = x.gelu() | |
gelu_approx() | Fast approximate GELU | y = x.gelu_approx() |
Binary Arithmetic
| Method | Operator | Description | Math | Example |
|---|---|---|---|---|
add() | + | Elementwise addition | x + y | |
sub() | - | Elementwise subtraction | x - y | |
mul() | * | Elementwise multiplication | x * y | |
div() | / | Elementwise division | x / y | |
floordiv() | // | Elementwise floor division | x // y | |
mod() | % | Elementwise modulus | x % y | |
pow() | ** | Elementwise exponentiation | x ** y | |
matmul() | @ | Matrix multiplication | x @ y | |
scaled_matmul(w, scale) | N/A | Matrix multiplication with output/input scale | or implementation-defined scaled GEMM | x.scaled_matmul(w, scale) |
min(y) | N/A | Elementwise minimum | x.min(y) | |
max(y) | N/A | Elementwise maximum | x.max(y) |
Comparison
| Method | Operator | Description | Example |
|---|---|---|---|
eq() | == | Elementwise equality | x == y |
ne() | != | Elementwise inequality | x != y |
lt() | < | Elementwise less-than | x < y |
le() | <= | Elementwise less-or-equal | x <= y |
gt() | > | Elementwise greater-than | x > y |
ge() | >= | Elementwise greater-or-equal | x >= y |
Logical & Bitwise
| Method | Operator | Description | Math | Example |
|---|---|---|---|---|
logical_and() | & | Elementwise AND | x & y | |
logical_or() | | | Elementwise OR | x | y | |
logical_xor() | ^ | Elementwise XOR | x ^ y | |
logical_not() | ~ | Elementwise NOT | ~x | |
bitwise_and() | & | Elementwise AND | x & y | |
bitwise_or() | | | Bitwise OR | x | y | |
bitwise_xor() | ^ | Bitwise XOR | x ^ y | |
bitwise_not() | ~ | Bitwise NOT | ~x | |
bitwise_shl() | << | Bitwise shift left | x << 1 | |
bitwise_shr() | >> | Bitwise shift right | x >> 1 |
Sampling
| Method | Description | Math | Example |
|---|---|---|---|
multinomial(k, replacement) | Sample indices from a categorical distribution | idx = probs.multinomial(k) |
Matrix Utilities
| Method | Description | Math | Example |
|---|---|---|---|
tril() | Lower-triangular matrix | x.tril() | |
triu() | Upper-triangular matrix | x.triu() |