There’s probably a more elegant solution, but you could do in manually, I suppose, as follows:
from torch.autograd import grad

# implement your model

# in your training loop:

    # fetch all intermediate outputs:
    outputs = model.forward(x_var, y_var)

    # compute cost for a particular layer
    layer_cost = layer_cost_fn(ouputs[some_index], y_true) 

    # compute gradients
    partial_derivatives = grad(layer_cost, (model.your_layer.weights, 
                                            model.your_layer.bias))

    # update model params
    model.your_layer.weights.data -= learning_rate * partial_derivatives[0]
    model.your_layer.bias.data -= learning_rate * partial_derivatives[1]