I ran the code and everything looks good. Let me see if I understand you correctly,
self.weight.repeat(self.in_features, 1) repeats each weight, say w1, 4 times. Autograd is smart enough to calculate dw1 by considering all activations affected by the copies of w1.
If this is correct, then you answered the question, amazing thanks! It seems to me this is non-trivial behaviour (I don’t know if you can guess this behaviour with the .repeat() method from looking at the tutorial where they only use simple operations like *).
p.s. after thinking about it a bit, I think it makes sense. repeated_weight either stores the python id of the self.weight object 4 times or maybe each element of the repeated_weight matrix has its own python id. Autograd figures out the contribution of the weights to the loss (out) through their python id?