bugger.
All challenges/Pythonhardgsoc-pytorch-optimizer-restore-v1
THE INVESTIGATION

The optimizer state restored to the wrong owner

Nested optimizer buffers retain their saved dtype and device even after parameter identities and templates change during checkpoint restoration.

MLHard MLPyTorchOptimizer stateCheckpoint ownership

What’s going wrong

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

A restorer maps saved parameter identities onto a new optimizer and copies its checkpoint, but its cast helper only recognizes tensor descriptors at the top level. Realistic state nests momentum and history inside dictionaries and lists. After a model changes device or floating dtype, those nested buffers remain assigned to the old template. Repair cast_state while preserving parameter-group mapping, global state and checkpoint isolation.

Input has checkpoint, live_groups and mutations. checkpoint is {"groups":[{"params":["old-w"],"lr":2}],"state":{"old-w":{"momentum":{"tensor":{"values":[1,2],"dtype":"float64","device":"cpu"}},"step":7}}}. live_groups is [{"params":[{"id":"weight","dtype":"float32","device":"cuda:1"}],"lr":9}]. There are 0 to 3 groups and at most 8 parameters total. Saved and live groups have equal counts, and matching groups have equal parameter counts. Map each saved parameter ID to the live parameter at the same group and position; never sort names. Saved IDs and live IDs are unique within their respective group lists.

Restored groups keep saved lr values but use live parameter IDs in the same group order. Each state key equal to a saved parameter ID is remapped to its live ID. Recursively visit that parameter’s state tree: a tensor descriptor is exactly {"tensor":{"values":[...],"dtype":"float64","device":"cpu"}}. Replace its dtype and device with the owning live parameter’s dtype and device, preserving its values. Recurse through ordinary dictionaries and lists; preserve all scalar metadata. The key tensor is reserved for complete descriptors. Objects with ordinary keys such as dtype, device and values but no tensor wrapper are metadata, not descriptors.

A state key not equal to any saved parameter ID is global state: copy it unchanged, including any tensor descriptors inside it. Global keys never collide with live parameter IDs. Parameters can have no saved state; do not synthesize an entry. Each restoration owns an independent deep copy, so modifying a loaded result cannot alter the checkpoint or a later reload.

mutations contains 0 to 8 objects {"key":"weight","path":["momentum"],"index":0,"value":9}. After restoring once, look up loaded.state[key], follow path through dictionaries using string parts and lists using nonnegative integer parts, then replace that descriptor’s tensor.values[index]. Paths have length 0 to 6 and always reach an existing descriptor and valid value index. Mutations run in array order and may target parameter or global state. Restore the original checkpoint again after all mutations. Print {"loaded":{"groups":[...],"state":{...}},"reloaded":{"groups":[...],"state":{...}},"checkpoint":{...}}. loaded includes the mutations; reloaded contains a fresh restoration; checkpoint is the original input checkpoint unchanged.

All inputs satisfy the shape and identity rules. Each parameter or global state tree has depth at most 6 and at most 128 nodes, counting ordinary containers and scalar values and treating a complete tensor descriptor as one leaf for these nesting bounds. Tensor values arrays contain 0 to 4 integers from -100 to 100. Other scalar metadata is null, boolean, an integer from -100 to 100, or an ASCII string of at most 16 characters. All names and dictionary keys are nonempty ASCII strings of at most 16 characters. Dtypes are float32 or float64, devices are cpu, cuda:0 or cuda:1, and lr is an integer from 0 to 100. This is metadata migration only: values do not change precision and no device operation, tensor math or optimizer update is performed.

Expected behavior

Restore each saved parameter’s complete nested state onto its live owner template while preserving global state, positional group mapping and independent checkpoint copies.

Reproduction

01 Nested momentum must follow its live owner without changing the checkpoint
{"checkpoint":{"groups":[{"params":["old-w"],"lr":2}],"state":{"old-w":{"momentum":{"tensor":{"values":[1,2],"dtype":"float64","device":"cpu"}},"step":7}}},"live_groups":[{"params":[{"id":"weight","dtype":"float32","device":"cuda:1"}],"lr":9}],"mutations":[{"key":"weight","path":["momentum"],"index":0,"value":9}]}
{"loaded":{"groups":[{"params":["weight"],"lr":2}],"state":{"weight":{"momentum":{"tensor":{"values":[9,2],"dtype":"float32","device":"cuda:1"}},"step":7}}},"reloaded":{"groups":[{"params":["weight"],"lr":2}],"state":{"weight":{"momentum":{"tensor":{"values":[1,2],"dtype":"float32","device":"cuda:1"}},"step":7}}},"checkpoint":{"groups":[{"params":["old-w"],"lr":2}],"state":{"old-w":{"momentum":{"tensor":{"values":[1,2],"dtype":"float64","device":"cpu"}},"step":7}}}}
02 A direct tensor state is a passing control
{"checkpoint":{"groups":[{"params":["old"],"lr":3}],"state":{"old":{"tensor":{"values":[3],"dtype":"float64","device":"cpu"}}}},"live_groups":[{"params":[{"id":"direct","dtype":"float32","device":"cuda:0"}],"lr":9}],"mutations":[]}
{"loaded":{"groups":[{"params":["direct"],"lr":3}],"state":{"direct":{"tensor":{"values":[3],"dtype":"float32","device":"cuda:0"}}}},"reloaded":{"groups":[{"params":["direct"],"lr":3}],"state":{"direct":{"tensor":{"values":[3],"dtype":"float32","device":"cuda:0"}}}},"checkpoint":{"groups":[{"params":["old"],"lr":3}],"state":{"old":{"tensor":{"values":[3],"dtype":"float64","device":"cpu"}}}}}
03 Each parameter owns its nested state while global state stays unchanged
{"checkpoint":{"groups":[{"params":["left"],"lr":1},{"params":["right"],"lr":4}],"state":{"left":{"buffers":[{"tensor":{"values":[2],"dtype":"float64","device":"cpu"}}]},"right":{"buffers":[{"tensor":{"values":[4],"dtype":"float32","device":"cuda:1"}}]},"global":{"timer":{"tensor":{"values":[6],"dtype":"float32","device":"cuda:1"}},"epoch":5}}},"live_groups":[{"params":[{"id":"lhs","dtype":"float32","device":"cuda:0"}],"lr":9},{"params":[{"id":"rhs","dtype":"float64","device":"cpu"}],"lr":8}],"mutations":[]}
{"loaded":{"groups":[{"params":["lhs"],"lr":1},{"params":["rhs"],"lr":4}],"state":{"lhs":{"buffers":[{"tensor":{"values":[2],"dtype":"float32","device":"cuda:0"}}]},"rhs":{"buffers":[{"tensor":{"values":[4],"dtype":"float64","device":"cpu"}}]},"global":{"timer":{"tensor":{"values":[6],"dtype":"float32","device":"cuda:1"}},"epoch":5}}},"reloaded":{"groups":[{"params":["lhs"],"lr":1},{"params":["rhs"],"lr":4}],"state":{"lhs":{"buffers":[{"tensor":{"values":[2],"dtype":"float32","device":"cuda:0"}}]},"rhs":{"buffers":[{"tensor":{"values":[4],"dtype":"float64","device":"cpu"}}]},"global":{"timer":{"tensor":{"values":[6],"dtype":"float32","device":"cuda:1"}},"epoch":5}}},"checkpoint":{"groups":[{"params":["left"],"lr":1},{"params":["right"],"lr":4}],"state":{"left":{"buffers":[{"tensor":{"values":[2],"dtype":"float64","device":"cpu"}}]},"right":{"buffers":[{"tensor":{"values":[4],"dtype":"float32","device":"cuda:1"}}]},"global":{"timer":{"tensor":{"values":[6],"dtype":"float32","device":"cuda:1"}},"epoch":5}}}}

Before you begin

  • Optimizer checkpoints
  • Parameter-group identity
  • Recursive container traversal
  • Deep-copy ownership
  • Tensor dtype and device metadata

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.