Before causal patching there were gradient attributions. Implement the two standard ones for a differentiable scalar model and check the completeness property that makes integrated gradients consistent.
grad_times_input(f, grad_f, x)
f(x) is a scalar function of a (d,) input and grad_f(x) returns its gradient. Return the (d,) attribution grad_f(x) * x.integrated_gradients(f, grad_f, x, baseline, steps)
baseline to x: with for (midpoint rule),
(d,) array.completeness_gap(f, grad_f, x, baseline, steps)
|sum(IG) - (f(x) - f(baseline))| as a float; it must shrink as steps grows.top_features(attr, k)
k largest attributions by absolute value, sorted by descending absolute value (ties by lower index).x.f the integrated gradients equal gradient-times-(x minus baseline) exactly and the gap is zero at any step count.f and grad_f as callables.