microsoft / microsoft/bocpy

`Tensor` indexing at Matrix parity

Open
#23 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

maths
Dominant language
Python
Stars
184
Forks
9
PR merge metrics
No merged PRs in 30d

Description

Problem

Tensor without a real subscript story is effectively write-only: a user can build one, do arithmetic on it, and aggregate it, but the moment they want "the first three rows" or "channel 0 of every frame" they have to drop back to constructing a fresh tensor and copying by hand. Matrix already accepts a sensible subset of NumPy keys; Tensor needs the same vocabulary lifted from rank 2 to arbitrary rank.

Desired functionality

Tensor.__getitem__ and Tensor.__setitem__ accept the same key shapes Matrix accepts today — int, slice, and tuples of those whose length is at most the tensor's rank — and behave the same way per axis. __getitem__ returns a new contiguous tensor of the appropriate shape. __setitem__ accepts either a scalar (broadcast to every targeted element) or a tensor whose shape exactly matches the indexed region.

Constraints

  • Results are copies. No views, no aliasing of the original storage.
  • Supported keys exhaust to: int, slice, tuple of (int | slice). Anything else raises TypeError naming the unsupported form.
  • Negative integer indices wrap; out-of-range raises IndexError.
  • __setitem__ does not invoke broadcasting on its right-hand side in v1: scalar broadcast yes, exact-shape Tensor yes, anything else TypeError.
  • Matrix is untouched by this work.

Out of scope

Ellipsis, None / np.newaxis, boolean masks, integer-array and list-of-indices fancy indexing, value-broadcast on __setitem__, views.

Open questions

  • Rank reduction. NumPy-style — tensor[i, :] on a rank-2 tensor returns rank 1 — versus Matrix-style — the same key returns rank 2 with an axis of size 1. The two are incompatible; one must be picked and documented. NumPy-style is the more conventional choice, but the project's existing rank-2 behaviour is the other one.
  • Empty-result slices: raise IndexError (current Matrix behaviour) or return a zero-size tensor.
  • Whether a rank-0 tensor accepts tensor[()] as identity.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the existing Matrix indexing behavior and the Tensor shape and storage entry points. Resolve the open questions about rank reduction, empty slices, and rank-0 indexing before implementing Tensor.getitem and Tensor.setitem. Done means supported integer and slice keys work across arbitrary ranks, copies remain contiguous and unaliased, assignment accepts only scalars or exact-shape tensors, and unsupported forms raise the specified errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.