Metadata-Version: 2.4
Name: dinox
Version: 0.7.0
Summary: Derivative Informed Neural Operators in JAX and Equinox
Author: Joshua Chen, Michael Brennan, Lianghao Cao, Thomas O'Leary-Roseberry
Author-email: Joshua Chen <joshuawchen@icloud.com>, Michael Brennan <mcbrenn@mit.edu>, Lianghao Cao <lianghao@caltech.edu>, Thomas O'Leary-Roseberry <tom.olearyroseberry@utexas.edu>
License-Expression: MIT
License-File: LICENSE
Requires-Dist: jax>=0.7.0
Requires-Dist: jaxlib>=0.7.0
Requires-Dist: jaxtyping
Requires-Dist: optax>=0.2.3
Requires-Dist: bayesflux[hippylib]>=0.10.0
Requires-Dist: equinox
Requires-Dist: hickle>=5.0.3
Requires-Dist: pydantic>=2
Requires-Dist: numpy
Requires-Dist: cupy-cuda12x ; extra == 'cupy'
Requires-Dist: nvidia-curand-cu12 ; extra == 'cupy'
Requires-Dist: pytest ; extra == 'dev'
Requires-Dist: black ; extra == 'dev'
Requires-Dist: isort ; extra == 'dev'
Requires-Dist: flake8 ; extra == 'dev'
Requires-Dist: flake8-pyproject ; extra == 'dev'
Requires-Python: >=3.11
Project-URL: Homepage, https://github.com/dinoSciML/dinox
Project-URL: Repository, https://github.com/dinoSciML/dinox
Provides-Extra: cupy
Provides-Extra: dev
Description-Content-Type: text/markdown

# dinox

`dinox` is a `JAX` implementation of Reduced Basis Derivative Informed Neural Operators, with CPU and single-GPU training.

The library is designed primarily for PDE learning workflows based on:

- `FEniCS 2019.1`
- Jacobians computed via `hippylib`
- Subspace methods provided by `bayesflux`

---

## Overview

`dinox` provides:

- Reduced basis neural operator architectures
- Derivative-informed training using PDE Jacobians
- GPU-accelerated implementations in `JAX` and `Equinox`
- Integration with FEniCS-discretized PDEs

---

## Changes in 0.7.0

- Moment-based reduction uses the finite-element mass matrix with the coefficient covariance obtained from the prior precision. Input-only and paired reductions support saving and reloading training, validation and test splits.
- Projected PDE solves accept an optional physical prior mean. Each serial rank pair draws its own samples. The parallel hIPPYlib generator reuses worker seeds across rank pairs.
- Dataset loading reads the requested rows from memory-mapped NumPy files before transferring arrays to JAX.
- DINO training executes the requested number of epochs, including partial validation intervals. New MLPs use the configured activation. Earlier dinox checkpoints retain the GELU activation used during their training.

## Getting Started

See the [hyperelasticity tutorial](https://github.com/dinoSciML/dinox/blob/main/examples/DINO_Tutorial.ipynb) for a complete walkthrough of the RB-DINO pipeline: problem setup, data generation, training with L2 vs H1 loss, and surrogate evaluation.

**Note on tutorial**: The tutorial requires access to the files in [dinox/examples](https://github.com/dinoSciML/dinox/tree/main/examples). Then one can open `examples/DINO_Tutorial.ipynb` to view the tutorial. We recommend the user perform these steps:

```bash
git clone --depth 1 --filter=blob:none --sparse https://github.com/dinoSciML/dinox.git
cd dinox
git sparse-checkout set examples
```

---

## dinox and LazyDINO

`dinox` is central to the repo [lazydino_tutorial](https://github.com/joshuawchen/lazydino_tutorial), which implements a tutorial jupyter notebook for the methods described in the paper: [LazyDINO](https://arxiv.org/abs/2411.12726)

### Coupled parameter and observation bases

Use `dimension_reduction_type="coupled"` to fit a parameter basis and an observation basis together. With Gaussian prior covariance $C$ and noise covariance $\Gamma$, BayesFlux alternately updates the two bases using sampled forward Jacobians in prior- and noise-whitened coordinates. Each requested pair of ranks gets its own fit. The algorithm and its information-theoretic interpretation are described by [Chen, Arnaud, Baptista and Zahm](https://arxiv.org/abs/2406.13425).

```python
from dinox.data_generating import generate_latent_data_for_dino_training

data = generate_latent_data_for_dino_training(
    N_trains=[64], key=7, dimension_reduction_type="coupled",
    model_obj=sampler, N_multiprocessing=0,
    input_output_dim_pairs=[(8, 3), (12, 4)],
    N_encodec_computation=100, N_test=16, N_val=16,
    save_path=data_path,
)
reduced_jacobians = data[(8, 3)]["train"]["encoded_Jacobians"]  # (64, 3, 8)
```

Here `sampler` implements the BayesFlux Gaussian sampler interface and supplies its prior and observation-noise precisions. The existing hIPPYlib workflow also accepts `model_name` and a positive `N_multiprocessing`. The usual DINO training and loading functions read the generated data from `data_path / "coupled"`. Load `encodec_dict.hkl` there for the physical encoders, decoders and convergence history for each pair. Set `coupled_max_iterations` or `coupled_tolerance` to control the alternating solve.

For sample rows, the parameter and observation coordinates are `x @ input_encoder` and `y @ output_encoder`. The target derivative is `output_encoder.T @ J @ input_decoder`. The observation encoder whitens the projected noise, so the reduced Gaussian likelihood uses identity noise covariance. If using centered coordinates, apply the same offsets to the training samples and to new parameters and observations. In particular, a nonzero prior mean maps to `prior_mean @ input_encoder` in the uncentered coordinates.

## Installation

dinox 0.7.0 requires Python 3.11 or later, JAX 0.7.0 or later, Pydantic 2 or later, and BayesFlux 0.10.0 or later. For CPU training on existing datasets or generation with a NumPy-compatible sampler, install the package in your Python environment.

```bash
python -m pip install dinox
```

For hIPPYlib PDE generation, install legacy FEniCS 2019.1 and its native DOLFIN, PETSc and MPI dependencies before installing dinox. The following conda command supports Linux and macOS. The additional CUDA steps apply to NVIDIA GPU training on Linux.

### Step 1 — Create a FEniCS 2019.1 environment

```bash
conda create -n fenics-2019.1_env -c conda-forge fenics==2019.1.0 python=3.11
conda activate fenics-2019.1_env
```

### Step 2 — Optional NVIDIA GPU setup

When using pip-provided CUDA libraries, remove conflicting CUDA paths from `LD_LIBRARY_PATH`. Consult the [JAX installation guide](https://docs.jax.dev/en/latest/installation.html) for supported platforms and drivers.

```bash
unset LD_LIBRARY_PATH
```

### Step 3 — Install GPU-enabled JAX on NVIDIA systems

```bash
pip install "jax[cuda12]" cupy-cuda12x nvidia-curand-cu12
```

### Step 4 — Install dinox

```bash
# With optional CuPy support on NVIDIA systems
pip install "dinox[cupy]"

# Without CuPy
pip install dinox
```

### Step 5 — Verify NVIDIA GPU availability

```bash
python -c "import jax; print('JAX devices:', jax.devices())"
python -c "import cupy; print('CuPy GPU count:', cupy.cuda.runtime.getDeviceCount())"
```

You should see your NVIDIA GPU listed. If JAX shows only `CpuDevice`, check that `LD_LIBRARY_PATH` is unset (see Step 2).

---

## GPU Support

- Designed for single-GPU workflows where all data fits in GPU memory
- Requires CUDA 12-enabled JAX (`pip install "jax[cuda12]"`) — the pip wheel bundles its own CUDA runtime
- Optional CuPy arrays for GPU operations via `dinox[cupy]`
- Without GPU, CPU fallback is automatic

---

## Development

```bash
conda create -n fenics-2019.1_env -c conda-forge fenics==2019.1.0 python=3.11
conda activate fenics-2019.1_env

pip install "jax[cuda12]" cupy-cuda12x nvidia-curand-cu12
unset LD_LIBRARY_PATH
pip install -e ".[dev]"
```

This installs:

- dinox (editable)
- development tools (pytest, black, flake8, isort)
- bayesflux[hippylib]
- hippylib
- all required JAX dependencies

---

## Requirements

- Python >= 3.11
- FEniCS 2019.1 for hIPPYlib PDE generation
- JAX >= 0.7.0 (for GPU: `pip install "jax[cuda12]"`)
- NVIDIA driver >= 525 (for GPU)
- Optional: CuPy for GPU array operations (`pip install dinox[cupy]`)

---

## Troubleshooting

| Problem | Solution |
|---|---|
| `Unable to load cuSPARSE` | `unset LD_LIBRARY_PATH` before running Python |
| `No such file: libcurand.so` | `pip install nvidia-curand-cu12` |
| JAX shows only `CpuDevice` | Ensure `jax[cuda12]` was installed (not just `jax`) and `LD_LIBRARY_PATH` is unset |
| `nvidia-smi` not found | Install or update NVIDIA driver (>= 525) |
| JAX/CuPy CUDA version conflict | Do **not** `conda install cudatoolkit` — let pip wheels provide CUDA |

---

## Repository

- Homepage: https://github.com/dinoSciML/dinox
- Repository: https://github.com/dinoSciML/dinox

For a nonzero physical prior mean, local versions support `projection_center=prior_mean` when generating a dataset with `reduce_input_before=True`. The forward model is evaluated at the affine projection through that mean. Saved input coefficients still use the original dinox convention `input_encoder.T @ m`.
