Originally published on Medium.
When combining an optimal control problem (OCP) with a PyTorch model, L4CasADi offers a useful solution. However, compiling the OCP can trigger an error:
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
This occurs after converting and compiling a PyTorch model in an OCP using L4CasADi. The problematic code pattern is:
import l4casadi as l4c
l4casadi_model = l4c.L4CasADi(pytorch_model, device=device)
The solution
Create a separate PyTorch model for the OCP instead:
import l4casadi as l4c
# copy model weight and bias from the original model
pytorch_model.load_state_dict(original_model.state_dict())
l4casadi_model = l4c.L4CasADi(pytorch_model, device=device)
This approach preserves gradient tracking by keeping distinct model instances — one for optimization and one for training.