Skip to content

Fix three incorrect gradient implementations (Softmax, RMSNorm, SimpleRNN)#7

Merged
sourcepirate merged 1 commit into
mainfrom
copilot/verify-gradient-accuracy
May 31, 2026
Merged

Fix three incorrect gradient implementations (Softmax, RMSNorm, SimpleRNN)#7
sourcepirate merged 1 commit into
mainfrom
copilot/verify-gradient-accuracy

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 31, 2026

Three gradient bugs producing silently wrong backpropagation results in the manual (no-autograd) training path.

Softmax — wrong Jacobian approximation (activations/softmax.py)

gradient() returned s * (1 - s) — the diagonal of the softmax Jacobian only. The off-diagonal terms (-s_i * s_j) are required for a correct chain-rule result. gradient() now raises NotImplementedError; gradient_fast() is rewritten to the closed-form vectorised formula:

# Before (wrong — diagonal only):
return self.last_output * (1 - self.last_output)

# After — full Jacobian contracted with upstream gradient:
dot = np.sum(s * grad_output, axis=-1, keepdims=True)
return s * (grad_output - dot)   # dL/dx_k = s_k * (g_k - Σ_i s_i·g_i)

This also replaces the O(n²) per-sample Jacobian loop with an O(n) broadcast.

RMSNorm — hardcoded reduction axes (layers/normalization/rmsnorm.py)

grads['weight'] reduction used axis=(0, 1), which only works for 3-D inputs (batch, seq, dim) and silently produces a scalar for 2-D (batch, dim) inputs:

# Before:
self.grads['weight'] = np.sum(grad_output * self.x_norm, axis=(0, 1))

# After — works for any input rank:
feature_axes = tuple(range(len(grad_output.shape) - 1))
self.grads['weight'] = np.sum(grad_output * self.x_norm, axis=feature_axes)

SimpleRNN — tanh derivative applied unconditionally (layers/recurrent/simple_rnn.py)

backward() always computed dz = dh * (1 - h²) regardless of the configured activation. For activation='linear' the correct derivative is 1, so dz = dh:

if self.activation_name == 'tanh':
    dz = dh * (1 - self.h_states[:, t+1, :]**2)
else:
    dz = dh  # linear: d(z)/dz = 1

@sourcepirate sourcepirate marked this pull request as ready for review May 31, 2026 10:49
Copilot AI review requested due to automatic review settings May 31, 2026 10:49
@sourcepirate sourcepirate merged commit 33d8cd8 into main May 31, 2026
3 checks passed
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants