Calculate gradient of the network's output w. r. t. a given vector

Hi all, I’m facing the following problem:

I have the output of the network y composed by N elements, say y_k for k=1,…,N and a vector p. I’d to calculate summation(on k) |dy_k/dp|.
This is the snippet of the code that I currently use:

...
y = model(data)
for y_i in y:
    for i in enumerate(p):
        grad[i]+=abs(autograd.grad(y_i, p[i], create_graph=True, retain_graph=True)[0])

Is there a way to do the same without iterate on y?
Something like:

...
y = model(data)
for i in enumerate(p):
    grad[i] = autograd(abs(y),abs(p[i]), create_graph=True, retain_graph=True)[0]

I have already tried different ways, but the final result doesn’t match the original one obtained with the first snippet of code.

Thanks in advance!