N-D broadcasting for `Tensor`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 184
- Forks
- 9
- PR merge metrics
- No merged PRs in 30d
Description
Problem
Once Tensor exists, almost every realistic workload runs into shape pairs that are obviously compatible without being identical: subtract a per-feature mean from every row, add a per-channel bias to a stack of images, scale a batch by a per-sample weight. Without broadcasting, the user has to allocate an expanded intermediate by hand — verbose, and exactly the kind of allocation that wipes out the benefit of running the op in C in the first place.
Matrix already has rank-2-only shortcuts for the row-vector and column-vector cases. The Tensor surface needs a general answer that subsumes them without regressing them.
Desired functionality
NumPy-style N-D broadcasting on every Tensor binary op. Shape pairs NumPy accepts are accepted; shape pairs NumPy rejects are rejected with an error message that names both operands' shapes and the axis where compatibility fails. The result of a broadcast op is a freshly-allocated contiguous tensor with the broadcast shape.
Constraints
- NumPy semantics, exactly. No bocpy-specific broadcasting rules.
- No views and no strides on the public Tensor surface — broadcasting is a compute-time concern, not a storage-time one.
- No measurable regression on rank-2
Matrixworkloads. Row-vector and column-vector broadcast shapes that hit a fast path today must still hit a fast path.
Out of scope
Reductions, batched matmul, stride-based views, broadcast_to / broadcast_arrays-style helpers, F-order layout, new ops.
Open questions
- Behaviour on zero-size shapes (any axis equal to zero).
- Whether broadcasting also applies to
Tensor.__setitem__right-hand sides, or whether scalar-broadcast remains the only accepted case there. Interacts with M4.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the Tensor binary-operation implementations and the existing Matrix row-vector and column-vector fast paths. Compare their behavior with NumPy broadcasting, including incompatible axes and zero-size shapes, and resolve the setitem scope question. Done means all Tensor binary ops produce contiguous broadcast-shaped results, report both shapes and the failing axis on errors, and preserve the existing rank-2 fast paths.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- machine-learning
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100