I would recommend you to do the following to avoid for loops and in-place ops:
D_ = D.view(B,C,-1)
D_max = D_.max(dim=2)[0].unsqueeze(2).unsqueeze(2)
D_norm = (D/D_max).view(*D.shape)

Anyway if you want to follow the for-loop approach you just need to use
D[b][c] = D[b][c].clone()

As you are modifying tensor’s value iteratively, which affects backprop.