#4879·burn

tensor: design proposal for a single Tensor type

Author: antimoraCreated Apr 26, 2026Updated Sep 29, 2026
Labelsdesignblocker

Context

We're already removing B from Tensor (#4717). After that lands, the signature is Tensor<D, K>. This proposal removes D and K too, leaving Tensor.

#3624 showed appetite for dynamic rank but tried to keep static rank alongside it, creating two parallel worlds. This proposal picks one: runtime rank and runtime dtype, same as every other tensor framework.

Remove Kind

Complex numbers don't fit. PR #3608 is the proof. Adding complex required a new crate, a decorator trait, and a layout abstraction - all to work around the fact that complex can't be a fourth kind without another ~80-method backend ops trait. Quantized tensors already couldn't justify a kind and got hacked in as a TensorPrimitive enum variant. The kind system doesn't scale.

With runtime DType, complex is two more enum variants. No new traits, no new crates, no decorator pattern.

Kind groups over the wrong axis. The real variation is dtype: f32 vs f16 vs i32 vs i8. That's what users think about, what backends dispatch on, what determines valid operations. Kind collapses these into Float/Int/Bool and forces explicit conversion across the boundary (bool_tensor.int().sum(), indices.float()). Meanwhile the distinction that matters within a kind (f32 vs f16) is handled at runtime anyway.

Backends implement the same logic three times. FloatTensorOps, IntTensorOps, BoolTensorOps - ~180 methods total. Many operations are identical across kinds: reshape, slice, gather, scatter, permute, expand, cat. Backends that already use one underlying type (TchTensor, FlexTensor) write the same code three times to satisfy the trait system.

The codebase knows it:

rust
// options.rs         — TODO: tensor kind enum?
// dispatch/backend   — TODO: either allow default dtype generic or remove associated types entirely?
// dispatch/qtensor   — TODO: this would be much cleaner if we consolidated tensor primitive types

Remove Rank

It doesn't deliver what it promises. Rust lacks const generic expressions. You can't write D - 1 as a return type. Every rank-changing operation (squeeze, unsqueeze, outer, matvec, det) takes a second const generic and validates the relationship at runtime:

rust
assert_eq!(R, D + 1, "`outer` with D={D} expects R={} (got R={R})", D + 1);

Panics, not compile errors. As @crutcher put it in #3624: "We're paying dependent type costs, without getting dependent type automatic features."

It blocks real broadcasting. NumPy/PyTorch broadcast shape(3,1) + shape(2,1,4) by prepending size-1 dims. Burn can't - operands must share D. Users manually unsqueeze before every cross-rank operation (#1499).

It pollutes everything. 203 const generic dimension parameters across burn-tensor/src/. 86 squeeze/unsqueeze calls in the API alone. Every nn module carries const D: usize through its forward signature. Linear special-cases 1D input with a manual unsqueeze/squeeze round-trip.

ONNX import can't always determine rank statically. Ranks depend on input data or dynamic axes. Burn APIs that require output rank as a const generic force the importer to guess or refuse to compile.

0-dimensional tensors remain unsupported (#1689). With runtime rank, a scalar is shape = [].

Result

rust
// Before (after #4717)
fn forward(x: Tensor<3>) -> Tensor<2> { ... }

// After
fn forward(x: Tensor) -> Tensor { ... }

DType and Shape are already runtime types. Operations already validate at runtime for every rank-changing op and for dtype resolution. This proposal makes the API honest about that.

Tradeoffs

We lose compile-time "wrong number of dimensions" and "float op on int tensor" checks. But:

  • Rank already punts to runtime for every rank-changing operation
  • Kind catches Float-vs-Int but misses the f32-vs-f16 distinction users actually care about
  • No other major tensor framework uses compile-time rank
  • Runtime shape contracts (#3624, @crutcher) can provide validation without infecting every type signature

Related

  • #4717 - Remove backend generic B (in progress)
  • #3608 - Complex backend (demonstrates cost of fitting complex into kind system)
  • #3624 - Dynamic rank tensor (motivating discussion)
  • #1499 - Broadcasting limitations due to static rank
  • #1689 - 0-dimensional tensor support

The trajectory is clear: Tensor<B, D, K> to Tensor<D, K> (#4717) to Tensor. Each step removes a generic that costs more than it gives.