Possible to set Torchscript Environment Variables when loaded in C++?

Hello! I’ve been working on running TorchModules in C++. I’ve been able to successfully trace the model and export a .ts package when the model is traced on the cpu (let’s call this “cpu.ts”). I’ve run into a snag when trying to trace the model using MPS on macOS.

When tracing the model with MPS, I hit the typical fallback error for a missing op which is fixed by adding PYTORCH_ENABLE_MPS_FALLBACK=1. After adding that environment variable to my local interpreter, the model is exported successfully to another .ts file (let’s call this one “mps.ts”).

If I load “cpu.ts” into my C++ program, it runs fine and responds to inputs correctly. However, if I load “mps.ts” into my C++ program, the program tosses the same error about a missing op and that I should enable PYTORCH_ENABLE_MPS_FALLBACK=1.

I compiled libtorch locally with USE_MPS and various metal flags. Do I need to add PYTORCH_ENABLE_MPS_FALLBACK=1 somewhere to the CMake build script, or is there a way to pass it to the program through C++?