bugger.
All challenges/Pythonhardgsoc-pytorch-strided-gradients-v1
THE INVESTIGATION

The strided gradient counted through two aliases

A view-backward routine must combine repeated output addresses without counting an expanded input’s shared storage more than once.

MLHard MLPyTorchAutogradTensor aliasing

What’s going wrong

ML practice with PyTorch internals. This is an original historical teaching reduction of a documented as_strided backward repair, not an actual upstream runtime. PyTorch core is included as ML debugging practice; no GSoC organization participation is implied.

Two tensor views refer to physical storage positions through their shapes, strides and offsets. The starter copies each output gradient into storage and reads it through the input view. That loses contributions when output positions alias, and overcounts contributions when several input positions share storage. Repair backward so both geometries obey the same gradient accounting rule.

Read {"input":{"shape":[2,3],"strides":[0,1],"offset":2},"output":{"shape":[2,2],"strides":[1,1],"offset":2},"grad":[2,4,6,8]} from stdin. A geometry has rank 1 to 3, equally long shape and strides arrays, shape entries from 0 to 4, nonnegative integer strides from 0 to 128, and an integer offset from 0 to 255. Each geometry has at most 32 logical elements. A logical element at indices i has physical address offset + sum(i[d]*strides[d]). Every existing logical element has an address from 0 to 255. Zero strides and repeated addresses are valid.

Enumerate indices in row-major order: the last dimension varies fastest. grad has one integer from -100 to 100 per output logical element in that order. Every output address is present among input addresses; the two logical shapes and element counts may differ. A zero dimension creates no elements. If input has no elements, output also has no elements. All input contracts hold.

For each physical address, first sum the gradients of every output logical element at that address. Then count how many input logical elements point to that address. The gradient returned for each input logical element is that storage sum divided by its input-address count. An input address with no output contribution has zero gradient. This averaging convention makes a later sum through the input aliases recover the storage contribution exactly once. Keep offsets as physical coordinates throughout; do not rebase the two views independently.

Print {"gradient":[1.0,5.0,4.0,1.0,5.0,4.0]} in input row-major logical order. Return floating-point values rounded using Python round(value, 6), as in the starter. Empty input returns an empty gradient array. The exercise computes one backward mapping; there is no actual tensor storage, GPU execution or general autograd engine.

Expected behavior

Scatter-add every output gradient by physical address, normalize by the input address’s alias multiplicity, and gather gradients in input logical order.

Reproduction

01 Output collisions and input aliases interact
{"input":{"shape":[2,3],"strides":[0,1],"offset":2},"output":{"shape":[2,2],"strides":[1,1],"offset":2},"grad":[2,4,6,8]}
{"gradient":[1.0,5.0,4.0,1.0,5.0,4.0]}
02 A nonoverlapping selection is a passing control
{"input":{"shape":[4],"strides":[1],"offset":0},"output":{"shape":[2],"strides":[2],"offset":0},"grad":[3,5]}
{"gradient":[3.0,0.0,5.0,0.0]}
03 A transposed output retains physical offsets
{"input":{"shape":[2,3],"strides":[3,1],"offset":4},"output":{"shape":[3,2],"strides":[1,3],"offset":4},"grad":[1,2,3,4,5,6]}
{"gradient":[1.0,3.0,5.0,2.0,4.0,6.0]}

Before you begin

  • Tensor shape and strides
  • Reverse-mode gradient accumulation
  • Aliased storage
  • Multidimensional index enumeration

Need a nudge?

Reveal one investigation hint at a time. Try each clue before opening the next.

    The bug is intentional. Run the starter code to see it fail, then work toward a fix. Difficulty is provisional.
    Read-only preview
    Ready when you are
    Every fix starts with a failing test.

    Read the report, then sign in to start your investigation.