In week 4 the compiler must decide which intermediate results to materialize, writing them to a temporary buffer with STAGE, and which to fuse into their consumers and recompute there. Implement the cost model that scores one such plan.
A graph maps node names to (op, inputs, numel). Ops:
"input": no inputs, already in memory."ew": elementwise over inputs of the same numel; costs one flop per element computed."expand" and "reshape": one input, each output element reads one input element; no flops."reduce": one input; each output element reads numel(input) // numel(output) input elements and costs one flop per element read.plan_cost(graph, outputs, staged, bw=1.0, peak=1.0). The materialized nodes are the inputs, the outputs and the staged nodes. Every materialized node that is not an input is computed by its own kernel:
numel(R) times. Walk back from through its inputs. If a node is evaluated times, each input of is read times (or numel(u) // numel(c) times when is a reduce).ew node, its evaluation count, plus, for each evaluated reduce node, the number of input elements it reads.numel(R) elements.Every element is 4 bytes. Return a dict with "kernels", "load_bytes", "store_bytes", "flops" and "time" = (load_bytes + store_bytes) / bw + flops / peak.