Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jit_compile pytorch does not work with values requiring gradients #137

Open
KarlisFre opened this issue Aug 17, 2023 · 3 comments
Open

jit_compile pytorch does not work with values requiring gradients #137

KarlisFre opened this issue Aug 17, 2023 · 3 comments

Comments

@KarlisFre
Copy link

Hi, I am trying to do gradient based training, unfortunately with jit_compile I get an error:

RuntimeError: Cannot insert a Tensor that requires grad as a constant. Consider making it a parameter or input, or detaching the gradient

from phi.torch.flow import *

object_geometry = Box(x=(40,60), y=(40, 60), z=(40, 60))
rotation_angle = torch.tensor((0.0, 0.1, 0.1),requires_grad=True)
object_geometry = object_geometry.rotated(rotation_angle)
OBSTACLE = Obstacle(object_geometry, velocity=(0.,0.,0.), angular_velocity=(0.,0.2, 0.2))

velocity = StaggeredGrid((0, 0, 0), 0, x=6, y=6, z=6, bounds=Box(x=100, y=100, z=100))
smoke = CenteredGrid(0, 0, x=6, y=6, z=6, bounds=Box(x=100, y=100, z=100))
INFLOW = 0.2 * resample(Sphere(x=50, y=50, z=10, radius=5), to=smoke, soft=True)
velocity, pressure = fluid.make_incompressible(velocity)

obst_mask = resample(OBSTACLE.geometry, smoke)
plot({"3D": obst_mask})
vis.show()

@jit_compile  # Only for PyTorch, TensorFlow and Jax
def step(v, s, p,obstacle, dt=1.):
    s = advect.mac_cormack(s, v, dt) + INFLOW
    buoyancy = resample(s * (0, 0, 0.1), to=v)
    v = advect.semi_lagrangian(v, v, dt) + buoyancy * dt
    v, p = fluid.make_incompressible(v, (obstacle))
    return v, s, p, obstacle


#for _ in view(smoke, velocity, 'pressure',obst_mask, play=True, namespace=globals(),port=6006).range(warmup=1):
for i in range(10):
    velocity, smoke, pressure, OBSTACLE = step(velocity, smoke, pressure, OBSTACLE)
    obst_mask = resample(OBSTACLE.geometry, smoke)
    print(i)
@holl-
Copy link
Collaborator

holl- commented Aug 17, 2023

This should work with Jax and TensorFlow.

JIT-compilation in PyTorch has many problems and pitfalls and is not actually that much faster. For fast code, I recommend jit-compiling with Jax or TensorFlow.

@KarlisFre
Copy link
Author

I was considering integrating phiflow with nimblephysics for solid motion and collision support https://github.com/keenon/nimblephysics. But nimblephysics supports only pytorch.

@holl-
Copy link
Collaborator

holl- commented Aug 21, 2023

I'm looking into fixing the PyTorch jit but this seems like a tricky problem. I'll get back to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants