Using the trained model of torch in ipopt

Hello.
I’ve trained a model in PyTorch using Dataset and DataLoader. Now, I need to use this trained model within CyIpopt, which relies on NumPy arrays for gradient-based optimization. Since PyTorch uses tensors, I have to constantly convert data between NumPy and PyTorch formats. Additionally, I’ve been using a DataLoader for inference, assuming it’s necessary for accurate outputs.

However, repeatedly converting data types and creating a DataLoader in every iteration is significantly slowing things down, making real-time implementation questionable. Is there a more efficient way to integrate the PyTorch model with CyIpopt to improve performance?

Are you cloning the array or are you using torch.from_numpy() to share the underlying data storage? The latter should be fast and I’m surprised if using a reference is already causing a bottleneck in your code.