Let’s say L is a transformation that takes a vector z as input. If your L can operate on multiple vectors at once, then you could do something like the following: (here, L squares all elements of the input):
z = torch.randn(3)
x = Variable(z.expand(3, 3), requires_grad=True)
out = (x ** 2).trace() # replace x ** 2 with L(x)
out.backward()
x.grad  # gives the derivatives matrix

The idea is to have each row contribute to out independently. The first row would contribute the first element of L to out, the second row contributes the second element of L, etc.