Skip to main content

tglfnn-ukaea

Neural network surrogate models of the TGLF quasilinear plasma turbulent transport simulator in various parameter spaces.

Paper for acknowledgment

If you use these models within your work, we request you cite the following paper:

Usage

Various different methods exist for using the models:

  1. Loading from PyTorch checkpoint
  2. Loading traced ONNX model
  3. Loading traced TorchScript model
  4. Loading the parameters directly into pure Python

Loading the traced TorchScript model allows the model to be used in Fortran (see below). Loading the parameters directly is a minimal-dependency method designed for use with other machine learning frameworks.

1. Loading from PyTorch checkpoint

import torch

# Load the model
efe_gb_model = torch.load('MultiMachineHyper_1Aug25/regressor_efe_gb.pt')

# Call the model
input_tensor = torch.tensor([[...]], dtype=torch.float32)  # Replace with appropriate input
output_tensor = efe_gb_model(input_tensor)

2. Loading traced ONNX model

import onnxruntime as ort

# Load the model
ort_session = ort.InferenceSession('MultiMachineHyper_1Aug25/regressor_efe_gb.onnx')

# Call the model
input_tensor = np.array([[...]], dtype=np.float32)  # Replace with appropriate input
outputs = ort_session.run(None, {'input': input_tensor})

3. Loading traced TorchScript model

import torch

# Load the model
torchscript_model = torch.jit.load('MultiMachineHyper_1Aug25/regressor_efe_gb_torchscript.pt')

# Call the model
input_tensor = torch.tensor([[...]], dtype=torch.float32)  # Replace with appropriate input
output_tensor = torchscript_model(input_tensor)

Using the traced TorchScript models in Fortran

The traced PyTorch models can be used in Fortran with FTorch, which provides Fortran bindings for LibTorch (the C++ backend of PyTorch). Please cite the Ftorch publication if using these models from Fortran.

Further details on the FTorch Implementation of these networks can be found in a related project.

Prerequisites

  • LibTorch: Download the appropriate version (CPU or GPU) from the PyTorch website and ensure it is accessible in your environment. CPU versions of the LibTorch and Pip packages have been tested. The LibTorch version requires no Python to install or run. It is suggested to look at the FTorch instructions below first.
  • FTorch: Install the FTorch library following the instructions in the FTorch repository. This also provides a compiler specific module (ftorch.mod).
  • Fortran Compiler: Use a modern Fortran compiler (e.g., gfortran or ifort) compatible with FTorch.
  • CMake: Version >= 3.1 required to build FTorch. Not essential, but helpful for building final Fortran code.

4. Loading the parameters directly into pure Python

The tglfnn_ukaea Python package distributes network parameters as plain NumPy arrays, with numpy as the only dependency. They are stored as tglfnn_ukaea/weights/{machine}/{version}.npz:

Machine Versions Default
multimachine 3.2.1 (MultiMachineHyper_1Aug25) 3.2.1
step 1.0.1 (JETTO-TGLF-EM-17Feb25) 1.0.1
import tglfnn_ukaea

params = tglfnn_ukaea.load("multimachine")  # default version, or e.g. version="3.2.1"
tglfnn_ukaea.available_versions("multimachine")  # ["3.2.1"]
params["version"]        # network version, e.g. "3.2.1"
params["input_labels"]   # input order expected by the network
params["efe_gb/layer_0/weight"]  # (num_estimators, out, in) float32

The parameters, normalisations and variance epsilon are exported directly from the ONNX models by scripts/onnx_to_npz.py, so they reproduce the ONNX outputs. That script documents the array layout and the forward pass. The normalisations come from the ONNX models, not stats.json, which differs from them. To regenerate the weights:

git lfs pull
pip install -e ".[export]"
python scripts/onnx_to_npz.py MultiMachineHyper_1Aug25 --version 3.2.1 \
    -o tglfnn_ukaea/weights/multimachine/3.2.1.npz --verify \
    --input-labels RLNS_1 RLTS_1 RLTS_2 TAUS_2 RMIN_LOC DRMAJDX_LOC Q_LOC SHAT XNUE KAPPA_LOC DELTA_LOC ZEFF VEXB_SHEAR
python scripts/onnx_to_npz.py JETTO-TGLF-EM-17Feb25 --version 1.0.1 \
    -o tglfnn_ukaea/weights/step/1.0.1.npz --verify \
    --input-labels RLNS_1 RLTS_1 RLTS_2 TAUS_2 RMIN_LOC DRMAJDX_LOC Q_LOC SHAT XNUE KAPPA_LOC S_KAPPA_LOC DELTA_LOC S_DELTA_LOC BETAE ZEFF

Reference outputs of the ONNX models are distributed as test data, for testing implementations of the networks. tglfnn_ukaea.load_reference("multimachine") returns random inputs and the ONNX models' (mean, variance) for each flux. They are generated by scripts/make_onnx_reference.py, which should be rerun whenever the weights are regenerated:

python scripts/make_onnx_reference.py

5. Compare TGLF and TGLFNN in JETTO production runs

  • TGLFNN is only an approximation of TGLF and it will make mistakes
  • scripts/tglf_vs_nn_jetto_trajectories.py shows how to plot the inputs and outputs spanned by TGLF and TGLFNN in a JETTO production run. NOTE: Available only in the following build on the JDC /home/tn2395/jintrac-devel

Metadata

Release files for tglfnn-ukaea 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tglfnn-ukaea 0.3.0
File Size Uploaded Provenance Actions
tglfnn_ukaea-0.3.0.tar.gz 118.9 MB Without provenance Details for tglfnn_ukaea-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tglfnn-ukaea 0.3.0
File Interpreter ABI Platform Actions
tglfnn_ukaea-0.3.0-py3-none-any.whl Python 3 none any Details for tglfnn_ukaea-0.3.0-py3-none-any.whl

Total release size: 237.8 MB

Release files / tglfnn_ukaea-0.3.0.tar.gz

Download URL tglfnn_ukaea-0.3.0.tar.gz
Size 118.9 MB
Tags Source
SHA-256 checksum
How to use checksums
a2b579ed2debc1d89694e392dfc39dc7f002c329a1a14eb2d9c64c0a616ac6dc
BLAKE2b-256 checksum
How to use checksums
fb7b6aeceba1f2f2b187c7c53db2f22c7a49d59b08701dcce7654b4d1726514b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.7

Release files / tglfnn_ukaea-0.3.0-py3-none-any.whl

Download URL tglfnn_ukaea-0.3.0-py3-none-any.whl
Size 118.9 MB
Tags Python 3
SHA-256 checksum
How to use checksums
41a22aa11cebd52b5a2be2fc13474043a38db88c79ed0969e2e0b67801e5de9e
BLAKE2b-256 checksum
How to use checksums
6befb994cadf1cd1bd5ed018848cae0cb994564641608a6d11439cccfcf835d5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.7

Security for tglfnn-ukaea 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page