If I understand you correctly, you want to calculate the gradients of a particular layer or upto a particular layer.
While i do not undertand the reason or benefit behind it, you can probably do that by calculating the loss directly from that particular layer, or make a subset of layers to calculate the gradient.