# Autograd inside torchscript from java (libtorch)

**URL:** <https://discuss.pytorch.org/t/autograd-inside-torchscript-from-java-libtorch/162326>\
**Category:** jit\
**Created:** [September 27, 2022, 6:57pm UTC](https://discuss.pytorch.org/t/autograd-inside-torchscript-from-java-libtorch/162326 "2022-09-27T18:57:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mhermon](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mhermon/32/45628_2.png) [@mhermon](https://discuss.pytorch.org/u/mhermon)\
**Post date:** [September 27, 2022, 6:57pm UTC](https://discuss.pytorch.org/t/autograd-inside-torchscript-from-java-libtorch/162326/1 "2022-09-27T18:57:31Z")

</div>

Hello, I am trying to use autograd to get gradients inside a torchscript file I am calling from java using libtorch but am running into an error “RuntimeError: element 0 of tensors does not require grad and does not have a grad\_fn”

My torchscript model:

```auto
class MyModel(torch.nn.Module):
    def __init__ (self):
        super(). __init__ ()
    
    def forward(self, x):
        # Require a gradient
        x.requires_grad_()
        y = x.pow(2).sum()
        print(f'y.requires_grad: {y.requires_grad}')
        # Check if x requires a gradient
        print(f'x.requires_grad: {x.requires_grad}')
        dy_dx = torch.autograd.grad([y], [x])[0]
        return dy_dx

```

Saving with:

```auto
model_scripted = torch.jit.script(MyModel())
model_scripted.save("model.pt")

```

This throws no errors and returns the expected values when I reload and run the model inside python with:

```auto
model_loaded = torch.jit.load("model.pt")
dy_dx = model_loaded(x)
print(dy_dx)

```

However this fails and throws an error when I load inside java and try to run the model:

```auto
Traceback of TorchScript, original code (most recent call last):
  File "/var/folders/g9/ykm_xvjx25s8bq5bkfjqy5240000gn/T/ipykernel_39052/954115032.py", line 12, in forward
        # Check if x requires a gradient
        print(f'x.requires_grad: {x.requires_grad}')
        dy_dx = torch.autograd.grad([y], [x])[0]
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
        return dy_dx
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

```

Not sure why this is a problem when loading the torchscript into java but not a problem when loading into python.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [September 28, 2022, 5:50am UTC](https://discuss.pytorch.org/t/autograd-inside-torchscript-from-java-libtorch/162326/2 "2022-09-28T05:50:46Z")

</div>

I guess you are trying to deploy the model onto a mobile platform?  
If so, I think gradient calculation is disabled by default as its main purpose is inference.

---

<div class="post-metadata">

**Author:** ![mhermon](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mhermon/32/45628_2.png) [@mhermon](https://discuss.pytorch.org/u/mhermon)\
**Post date:** [September 29, 2022, 4:32pm UTC](https://discuss.pytorch.org/t/autograd-inside-torchscript-from-java-libtorch/162326/3 "2022-09-29T16:32:43Z")

</div>

Hey @ptrblck thanks for the response. No, this is not to a mobile platform. Just trying to run the torchscript model inside a java application.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [September 29, 2022, 4:51pm UTC](https://discuss.pytorch.org/t/autograd-inside-torchscript-from-java-libtorch/162326/4 "2022-09-29T16:51:06Z")

</div>

Ah OK, I’m not familiar with this workflow and don’t know what the Java env might set and if it would disable gradient calculation.

---

<div class="post-metadata">

**Author:** ![saudet](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/saudet/32/37184_2.png) [@saudet](https://discuss.pytorch.org/u/saudet)\
**Post date:** [November 10, 2022, 3:44am UTC](https://discuss.pytorch.org/t/autograd-inside-torchscript-from-java-libtorch/162326/5 "2022-11-10T03:44:48Z")

</div>

Does it do the same if you try to load it with the JavaCPP Presets for PyTorch?

> **[javacpp-presets/pytorch at master · bytedeco/javacpp-presets](https://github.com/bytedeco/javacpp-presets/tree/master/pytorch)**
>
> master/pytorch

> <https://github.com/bytedeco/javacpp-presets/blob/master/pytorch/samples/ExampleApp.java>

---

<div class="post-metadata">

**Author:** ![mhermon](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mhermon/32/45628_2.png) [@mhermon](https://discuss.pytorch.org/u/mhermon)\
**Post date:** [December 2, 2022, 9:43pm UTC](https://discuss.pytorch.org/t/autograd-inside-torchscript-from-java-libtorch/162326/6 "2022-12-02T21:43:25Z")

</div>

Attempting to use the JavaCPP Presets I run into an issue with JNITorch:

```auto
Warning: Version of org.bytedeco:pytorch could not be found.
Warning: Version of org.bytedeco:openblas could not be found.
 Uncaught exception: exiting with status code 1
java.lang.UnsatisfiedLinkError: no jnitorch in java.library.path

```

---

<div class="post-metadata">

**Author:** ![saudet](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/saudet/32/37184_2.png) [@saudet](https://discuss.pytorch.org/u/saudet)\
**Post date:** [December 3, 2022, 12:49am UTC](https://discuss.pytorch.org/t/autograd-inside-torchscript-from-java-libtorch/162326/7 "2022-12-03T00:49:29Z")

</div>

If you’re trying this on Android, we’d first need to create builds for that platform.

---

<div class="post-metadata">

**Author:** ![dabs](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dabs/32/46105_2.png) [@dabs](https://discuss.pytorch.org/u/dabs)\
**Post date:** [April 28, 2023, 9:35pm UTC](https://discuss.pytorch.org/t/autograd-inside-torchscript-from-java-libtorch/162326/8 "2023-04-28T21:35:03Z")

</div>

@mhermon I am running into a similar issue. Did you find a solution?
