cs.LGAug 9, 2026

Memory-Efficient Activation Checkpointing with Sliding Window and Hirschberg's Algorithm for 0/1 Knapsack Solving in PyTorch

Authors: Jędrzej Maczan

Organizations: Cohere Labs Community Poland

Abstract

Activation checkpointing minimizes the runtime of neural networks under a given memory budget, by selecting which intermediate tensors to store and which to recompute. PyTorch solves this as a 0/1 knapsack problem, where operations from a joint forward-backward computation graph are items with a memory cost (weight) and a runtime saving (value). The default solver, dp_knapsack, allocates a full dynamic programming (DP) table of shape (n+1)×(W+1)(n+1) \times (W+1), where nn is the number of operations and WW is the quantized memory budget. This method is resource-hungry and crashes at n=100n = 100 items on a machine with 64 GB RAM. In this paper, we introduce dp_knapsack_sliding_hirschberg, which combines the sliding window trick and Hirschberg's algorithm to reduce peak memory from O(nW)O(nW) to O(W)O(W) while preserving the exact optimal solution. Our experiments show successful knapsack execution at n=2000n = 2000, where dp_knapsack fails at n=100n = 100, a 20×\times increase in computable problem size. In addition, our benchmarks show a consistent 25-28% runtime speedup over dp_knapsack. The implementation is merged into PyTorch and released in version 2.10.

Explore similar work

CardsList
  1. Gefen: Optimized Stochastic Optimizer

    Jun 11, 2026Nadav Benedek, Tomer Koren, Ohad FriedAdamScaling Laws