Convert a Julia function to a torch.autograd.Function
- Dominant language
- Julia
- Stars
- 240
- Forks
- 18
- PR merge metrics
- No merged PRs in 30d
Description
### Motivation and description
I want to use an ODESolver in a PyTorch ML stack.
It's pretty easy to write a Julia function that takes in initial conditions of a differential equation, uses the DifferentialEquations.jl to solve that differential equation, and returns the results of that equation. That Julia function will be automatically differential, and I'd like to be able to convert it into a PyTorch-compatible object (with gradient information preserved).
### Possible Implementation
```python
###################
#### Package ######
###################
from juliacall import Main as jl
import numpy as np
import torch
from torch.autograd import Function, gradcheck
loss = jl.seval("loss(f, grad) = x -> (sum(pyconvert(Array, f(x)) .* grad))")
try:
gradient = jl.seval("using ForwardDiff: gradient; gradient")
except:
jl.seval("import Pkg; Pkg.add(\"ForwardDiff\")")
gradient = jl.seval("using ForwardDiff: gradient; gradient")
class CallJuliaFunction(Function):
@staticmethod
def forward(ctx, f, x):
ctx.f = f
ctx.save_for_backward(x)
np_x = x.detach().numpy()
jl_res = f(np_x)
np_res = np.array(jl_res)
torch_res = torch.from_numpy(np_res)
return torch_res
@staticmethod
def backward(ctx, grad_output):
f = ctx.f
x, = ctx.saved_tensors
np_x = x.detach().numpy()
np_grad_output = grad_output.detach().numpy()
ls = loss(f, np_grad_output)
jl_grad = gradient(ls, np_x)
np_grad = np.array(jl_grad)
torch_grad = torch.from_numpy(np_grad)
return None, torch_grad
###################
##### Tests #######
###################
x = torch.randn(3,3,dtype=torch.double,requires_grad=True)
f = jl.seval("f(x) = 2 .* x")
f2 = lambda x: f(x) # hack to work around https://github.com/JuliaPy/PythonCall.jl/issues/390
# Use it by calling the apply method:
print(x)
output = CallJuliaFunction.apply(f, x)
print(output)
output = CallJuliaFunction.apply(f2, x)
print(output)
# gradcheck takes a tuple of tensors as input, check if your gradient
# evaluated with these tensors are close enough to numerical
# approximations and returns True if they all verify this condition.
input = (f2, torch.randn(3,3,dtype=torch.double,requires_grad=True),)
test = gradcheck(CallJuliaFunction.apply, input, eps=1e-6, atol=1e-4)
print(test)
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the Python prototype in the issue, especially CallJuliaFunction and its torch.autograd.Function and gradcheck usage. Determine how this Julia-to-PyTorch bridge should fit Torch.jl and what behavior must be preserved for Julia functions and gradients; done means a supported approach is demonstrated and validated beyond the example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- julia, python, pytorch
- Domain
- machine-learning
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100