Gradient checkpointing forwardEasy

Gradient checkpointing forward

Background

Gradient checkpointing (a.k.a. activation recomputation) trades compute for memory. A normal forward pass stores every layer's intermediate activation so the backward pass can reuse them — memory grows linearly with depth. Checkpointing instead discards intermediates during the forward pass and recomputes them on demand during backprop. This problem models the forward half: apply a chain of layer functions in sequence, returning only the final output (no intermediates retained).

Problem statement

Implement checkpoint_forward(funcs, input_arr) that applies each function in funcs to the running array in order and returns the final result as a float array:

x0=input,xi=fi(xi1),return xnx_0 = \text{input}, \qquad x_i = f_i(x_{i-1}), \qquad \text{return } x_n

No intermediate xix_i is kept — only the running value is carried forward.

Input

  • funcs — a list of callables [f1,f2,,fn][f_1, f_2, \dots, f_n], each mapping an array to an array.
  • input_arr — an np.ndarray input x0x_0.

Output

An np.ndarray (cast to float) equal to fn(f2(f1(x0)))f_n(\dots f_2(f_1(x_0))).

Examples

Example 1

Input:  f1(x)=x+1, f2(x)=x*2, f3(x)=x-3, input_arr = [1.0, 2.0]
Output: [1.0, 3.0]

Explanation: [1,2]+1[2,3]×2[4,6]3[1,3][1,2]\xrightarrow{+1}[2,3]\xrightarrow{\times2}[4,6]\xrightarrow{-3}[1,3]. Only the final array is returned; the two intermediate arrays are not stored.

Constraints

  • Apply the functions strictly left-to-right (function composition order).
  • Carry a single running variable; do not accumulate a list of intermediates.
  • Return the result cast to float.

Notes

  • The memory win is asymptotic: storing O(1)O(1) activations instead of O(n)O(n) at the cost of recomputing the forward pass during backprop (roughly one extra forward).
  • In real frameworks, checkpointing wraps segments of layers; the recompute happens lazily inside the backward graph.
Python
Loading...

This problem ships 4 hidden tests. They run in your browser via Pyodide — no backend, no submission queue. Press ▶ Run tests to execute.

  • Reference example
  • Empty function list returns the input (as float)
  • Output is cast to float
  • Composition order is left-to-right