Implement recompute_plan(ops, graph_inputs, saved, annotated, sizes) for tensor-level activation checkpointing: some saved tensors are annotated, freed after the forward pass and recomputed later. Find the ops each one needs and the storage actually freed.
Inputs:
ops: topologically ordered list of {"name": str, "inputs": [names], "view": bool}; op name produces tensor name. "view" is optional (default False); a view's output shares storage with its first input.graph_inputs: always in memory. saved: kept for backward. annotated: subset of saved that is freed. sizes: bytes per tensor name.Definitions:
inputs[0] while the producing op is a view; the first non-view tensor reached is the root (graph inputs are their own roots).Return a dict:
"recompute": for each annotated name (in annotated order) the ops that rebuild it from available tensors: its own op plus, recursively, the op of every unavailable input, in topological order."unique_ops": the union of those lists, in topological order."freed_storages": roots for which every graph input and every saved tensor living on that root is annotated, in topological order of the root."memory_saved": sum(sizes[root]) over freed_storages, as an int.