The elementwise multiplication is a radical simplification of the compute involved in the the chain rule. You could probably do something like dloss_dw = torch.diagonal_embedding(dmodel_dw(...)) @ dloss_dtp to be closer to the chain rule but less computationally efficient.
The summation is from using the same term multiple times, here from broadcasting. You can try this yourself on paper by spelling out the gradients per element (for two elements). Or you could have a simpler version with just some scalar g(x,y) and try to compute the derivative of f(x) = g(x,x) to see the same abstract principle.

Best regards
Thomas