Skip to content

Install and verify differentiable simulation

Use a source checkout that contains docs/tutorial/code/differentiable_particle.py. The commands below build that checkout; installing pyuipc from PyPI does not select a local Git branch. The native adjoint runs on the CUDA backend. Python examples also need the Python bindings. PyTorch is optional: the native NumPy example and ordinary forward simulations do not import it.

Build for your Python environment

Follow the compiler, CUDA Toolkit and vcpkg setup for Windows or Linux first. Activate a dedicated Python environment and run the following commands from the repository root. The non-wheel build replaces any existing pyuipc installation in the selected interpreter when it stages the new bindings. For a fresh environment, run python -m venv .venv-diff, then activate it with .\.venv-diff\Scripts\Activate.ps1 in PowerShell or source .venv-diff/bin/activate in Bash. This keeps a reference build in another interpreter untouched.

$pythonExe = python -c "import sys; print(sys.executable)"
cmake -S . -B build-diff -DCMAKE_BUILD_TYPE=Release -DUIPC_WITH_CUDA_BACKEND=ON -DUIPC_BUILD_PYBIND=ON -DUIPC_BUILD_PYTHON_WHEEL=OFF "-DUIPC_PYTHON_EXECUTABLE_PATH=$pythonExe"
cmake --build build-diff --config Release --target pyuipc
python_exe="$(python -c 'import sys; print(sys.executable)')"
cmake -S . -B build-diff -DCMAKE_BUILD_TYPE=Release -DUIPC_WITH_CUDA_BACKEND=ON -DUIPC_BUILD_PYBIND=ON -DUIPC_BUILD_PYTHON_WHEEL=OFF "-DUIPC_PYTHON_EXECUTABLE_PATH=$python_exe"
cmake --build build-diff --config Release --target pyuipc

There is no separate differentiable CMake switch. The per-scene setting diff_sim/enable defaults to off. Creating a DiffSimParameter or accessing the mutable scene.diff_sim() enables the features for that scene; do this before world.init(scene).

Check the loaded build and run a backward pass

Use the same active interpreter for the checks below:

python -c "import sys, uipc; print(sys.executable); print(uipc.__file__); print(uipc.pyuipc.__file__)"
python -m uipc doctor --probe-cuda
python docs/tutorial/code/differentiable_native.py

The first command shows the interpreter, Python package and native extension paths. The doctor checks whether the CUDA backend loads. The last command tests more: it runs two frames and differentiates the terminal-position loss with respect to the initial velocity. It should print x = 0.02000000, dL/dP = -0.00200000.

For the PyTorch interface, install PyTorch in this environment if needed, then run the optimizer example:

python -m pip install torch
python docs/tutorial/code/differentiable_particle.py

Expected: positions [0.2, 0.4], design gradient dL/dU = -0.080000, and terminal position 0.432000 after one optimizer step. CPU PyTorch tensors are sufficient for this example; the simulation still needs a CUDA-capable GPU. This example uses a different parameter and loss from the NumPy example, so the two gradient values are not supposed to match.

Troubleshooting

  • import uipc works but uipc.torch or a native feature is missing. Check the three paths above and python -m pip show pyuipc. A different interpreter or an old package earlier on PYTHONPATH can hide the build. If only torch is missing, install it in the interpreter you just checked.
  • The doctor cannot load CUDA. Resolve the backend or driver issue reported by the probe before debugging gradients. Installing PyTorch does not supply libuipc's native simulation backend.
  • Both examples pass but your scene cannot run backward. Register its bindings before initialization, use BDF1, and record a converged trajectory starting at frame zero. A nonzero checkpoint alone cannot reconstruct the preceding gradient history. See the supported input and solver conditions.

Continue with the PyTorch optimization tutorial or the C++ and NumPy interface.