Skip to content

Commit ac714ff

Browse files
committed
Assert the gradients in the tiled-logits layout test
The flatten under test also feeds the backward: x_grad is a zeros_like of the flattened activation, scattered per shard through x_grad.narrow(...).view_as(x_shard) and unflattened on the way out. The test compared only the loss and never called backward, so that path was not exercised and the copy reshape makes for a non-contiguous input was never checked for putting the gradient back where it came from. shards=2 loss_equal=True x_grad_equal=True max|dx|=0.000e+00 max|dW|=0.000e+00 shards=4 loss_equal=True x_grad_equal=True max|dx|=0.000e+00 max|dW|=0.000e+00 Also asserts the parameter gradients, which is the third comparison TestTiledMLPInputLayout makes. Signed-off-by: alanhuangyoo <alanhuangyoo@gmail.com>
1 parent 307ff15 commit ac714ff

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

tests/unit/ulysses_alst/test_tiled_compute.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,10 +435,17 @@ def test_transposed_input_matches_a_contiguous_copy(self,
435435
y = torch.randint(0, vocab_size, (batch_size, seqlen))
436436

437437
model = self.make_model(hidden_dim, vocab_size, dtype)
438-
losses = []
438+
losses, param_grads = [], []
439439
for x in (strided, contiguous):
440440
model.zero_grad()
441441
loss = TiledFusedLogitsLoss.apply(self.loss_fn, model, x, y, None, shards, list(model.parameters()), "sum")
442+
# The backward scatters into a zeros_like of the same flattened activation, so the
443+
# gradient path runs through the flatten under test as well as the forward.
444+
loss.backward()
442445
losses.append(loss)
446+
param_grads.append([p.grad.detach().clone() for p in model.parameters()])
443447

444448
torch_assert_close(losses[0], losses[1])
449+
torch_assert_close(strided.grad, contiguous.grad)
450+
for grad_a, grad_b in zip(*param_grads):
451+
torch_assert_close(grad_a, grad_b)

0 commit comments

Comments
 (0)