When tracing a computation graph for activation checkpointing, many operations produce tensors that share the same underlying memory storage with earlier tensors (e.g., reshape, view, transpose, squeeze / unsqueeze). Recomputing such tensors during the backward pass is wasteful because they are merely different metadata interpretations of the same bytes already available in memory.
Implement a function deduplicate_recomputation(nodes) that analyzes a traced subgraph and removes redundant recomputation nodes that alias existing storage.
Input: nodes is a list of dicts representing nodes in traversal order. Each node is:
id (int): the node's unique identifierstorage_ptr (int): pointer/identifier for the tensor's underlying storagecost (float): recomputation FLOP cost of that nodeRule: For each distinct storage_ptr, keep only the first node (in list order) that produces it. All later nodes sharing that same storage_ptr are considered aliasing duplicates and should be pruned. Their cost is added to the total savings.
Return a single dict with keys:
kept_ids: list of kept node ids in the original ordertotal_cost_saved: total recomputation cost saved by removing duplicates