Yes, in MatLab code, before joining the gradients, they normalize the two gradients. I think the aim of using normalization is to scale the two gradients to the same magnitude.
if norm(d1(:))>norm(d2(:))
    d1 = d1*norm(d2(:))/norm(d1(:));
else
    d2 = d2*norm(d1(:))/norm(d2(:));
end

If the norm of d1 is bigger than d2, it will scale d1 to the same magnitude of d1. Because d1 and d2 have the same magnitude, the kappa will work better.
So, How does PyTorch perform the same with this MatLab code?