Skip to content

About

A hyperparameter optimization framework for .NET

Resources

Code of conduct

Contributing

Security policy

Stars

1 star

Watchers

2 watching

Forks

Repository files navigation

OptunaSharp

OptunaSharp

Optuna for .NET

Build Docs NuGet License

OptunaSharp is a faithful and complete .NET port of Optuna, an automatic hyperparameter optimization software framework, particularly designed for machine learning.

OptunaSharp is thread-safe and truly parallel.
nJobs runs trials on all your cores at once, with no GIL in the way, and workers on
many machines share one study, Python Optuna workers included.
OptunaSharp supports parallelism powered by:
single-threaded, reused dedicated threads or the .NET ThreadPool.

OptunaSharp is cloud-ready, with supported integrations for:
Artifact storage - Amazon S3 · Azure Blob Storage · Google Cloud Storage · Cloudflare R2
Study storage - PostgreSQL · MySQL · Redis · gRPC storage proxy

The compatibility target is Optuna 5.x. When OptunaSharp behavior and Optuna
behavior differ, Optuna is treated as the source of truth and OptunaSharp aligns to it.

Features

OptunaSharp is a complete 100% port of Optuna 5.x with full and backwards compatability with optuna's integrations.

  • Define-by-run API - Study, Trial and FrozenTrial with every suggest_*, Optimize, ask-and-tell, enqueued trials, multi-objective directions, constraints, user attributes and callbacks; float, int and categorical distributions with search-space inference. All of it lives in the pure-managed Core package, built on NumSharp.

  • Samplers - Optuna's Random, Grid, TPE, CMA-ES, GP, QMC, BruteForce, PartialFixed, NSGA-II and NSGA-III, plus samplers Optuna does not have: AGE-MOEA-2, SMS-EMOA, MOEA/D, RVEA, LM-CMA, VkD-CMA, differential evolution, PSO, the natural evolution strategies (OpenAI-ES, SNES, xNES), TuRBO, REMBO, SAASBO and JANUS (with its DE and multi-objective variants).

  • Integrations - a native scikit-learn-style OptunaSearchCV, LightGBMTuner, ML.NET, training-loop pruning callbacks, Microsoft.Extensions.DependencyInjection registrations, and the optunasharp command-line tool, a port of the optuna CLI.

  • Python bridges - OptunaHub packages, BoTorch, pycma and pymoo, driven in-process through pythonnet.

  • Pruners - Median, Percentile, SuccessiveHalving, Hyperband, Threshold, Patient, Wilcoxon and Nop.

  • Analysis - parameter importance (fANOVA, mean decrease impurity, PED-ANOVA and SHAP, all pure-managed and byte-exact), terminators, and the data behind all 12 plot_* functions, which the Plotly.NET package draws as interactive figures.

  • Storage - in-memory and journal-file storage in Core; SQLite, PostgreSQL / MySQL, Redis journals and a gRPC storage proxy as companion packages. Python Optuna can work on the same study through the journal file, any of the databases, Redis or the gRPC proxy. On the SQLite, PostgreSQL and MySQL storages, heartbeats with RetryFailedTrialCallback retry the trials of a worker that died.

  • Artifacts - a file-system store in Core; Amazon S3, Google Cloud Storage, Azure Blob Storage and Cloudflare R2 as companion packages.

  • Parallel and thread-safe - Optimize(nJobs: n) runs n trials at once, with no GIL in the way. Measured on 10 cores (20 logical CPUs), 64 trials of a ~50 ms CPU-bound objective finished 8.0× faster at nJobs: 8 and 10.3× faster at nJobs: 16 than at nJobs: 1, while Python Optuna's time stays flat because the GIL runs one Python thread at a time (Performance).

    • Trials run on OptunaSharp's own dedicated threads by default, or on the .NET ThreadPool.
    • Sampling is serial per sampler, as in Optuna, unless SamplerConcurrency.PerWorker gives each worker its own Random, TPE, CMA-ES, VkD-CMA or Grid sampler.
    • A CancellationToken, or StopSignals for Ctrl+C and SIGTERM, stops a run gracefully: no new trial starts, and the trials in flight finish and are stored.
    • Studies, trials, samplers, pruners and storages are safe to share between threads; docs/THREADING.md has the design, its guarantees and its limits.
  • Reproducible and tested - a seeded run is bit-identical to Python wherever the path is deterministic, enforced by a live-Optuna parity harness (pythonnet) that asks real Optuna for each answer in-process and asserts that OptunaSharp matches. Everything builds and tests on net8.0 and net10.0 across Windows, Linux and macOS, behind strong-naming and package gates, and all 20 packages release together from one tag.

Scientific Lead and Contributions

OptunaSharp has reimplemented byte-for-byte many cutting-edge papers and state of the art algorithms and will continue to doing so.
Found an incredible paper? You believe a method/optimizer/sampler/technique has achieved a significant breakthrough or has been referenced by a bunch of papers?

Open an issue or merge request!


OptunaSharp implements these methods based on papers and their original implementation (if there is one):

AGE-MOEA-2, SMS-EMOA, MOEA/D, RVEA, LM-CMA, VkD-CMA, differential evolution, PSO, the natural evolution strategies (OpenAI-ES, SNES, xNES), TuRBO, REMBO, SAASBO and JANUS (with its DE and multi-objective variants).

And we would love to accept more!

What is Optuna?

Optuna is an automatic hyperparameter optimization framework with an imperative, define-by-run API. The two core concepts:

  • Study - an optimization session over an objective function.
  • Trial - a single evaluation of the objective, during which hyperparameters are suggested.
# Optuna (Python) - the behavior OptunaSharp targets
import optuna

def objective(trial):
    x = trial.suggest_float("x", -10, 10)
    return (x - 2) ** 2

study = optuna.create_study(sampler=optuna.samplers.TPESampler(seed=42))
study.optimize(objective, n_trials=20)
print(study.best_value, study.best_params)

The C# quick start below is the same study, written against the same define-by-run shape: snake_case names become PascalCase (suggest_float → SuggestFloat, n_trials → nTrials), while parameter order, defaults and error messages are kept as Optuna has them.

Documentation

The documentation site, scisharp.github.io/OptunaSharp:

Performance

Measured on 2026-11-23 on an Intel Xeon E5-2690 v2 server (Ivy Bridge: 10 cores, 20 logical CPUs, 3.0 GHz with turbo to 3.6 GHz, AVX without AVX2) under Linux. OptunaSharp ran as .NET 10.0.15 Release builds, and Python 3.12.16 ran Optuna 5.0.0 with NumPy 2.5.4.

The times are steady-state. Each process first repeats its workload until its runtime has settled and discards those runs: .NET for at least 70 runs, past both steps of its tiered compilation, then until a quarter of them compiled nothing; Python until a run imports nothing. It then times 3 to 30 runs. Each workload ran in five fresh processes, the libraries taking turns in an order reversed every round. A time is the mean of the middle three processes' medians, so the fastest and the slowest process are dropped. Its 95 % confidence interval is within ±2 % for 63 of the 72 times and rates below and within ±3.2 % for all; the executors' delays of about 2 ms vary by up to ±6 %.

Every process started on an idle machine, and was run again if another guest or session on the host used half a CPU while it ran. Studies that run one trial at a time were pinned with taskset to two dedicated cores whose hyperthread siblings stayed idle: one core for the main thread, one for the runtime's helpers (.NET's JIT and garbage collector, OpenBLAS). Parallel runs had all 20 logical CPUs. bench/ has the harness, the method and its checks. Absolute times depend on the hardware, so compare within a row.

Parallel trials

64 trials of a CPU-bound objective that takes about 50 ms on one core, with RandomSampler. Each language calibrates its loop to ~50 ms in every process. The speedup compares each process's own nJobs: 1 time.

nJobs OptunaSharp Speedup Python Optuna 5.0
1 3.22 s 1.0× 3.10 s
2 1.60 s 2.0× 3.15 s
4 0.80 s 4.0× 3.16 s
8 0.40 s 8.0× 3.16 s
16 0.31 s 10.3× 3.18 s

Python Optuna's time stays flat because the GIL runs one Python thread at a time. Trials that wait rather than compute run all at once in both: 640 trials sleeping 100 ms took 1.00 s at nJobs: 64, and 1,280 took 1.01 s at nJobs: 128 (Python Optuna: 1.04 s and 1.09 s; ideal 1.0 s).

Samplers

One trial at a time, the same seeded study in both libraries, on a cheap sphere objective so that the sampler's own work dominates.

Study OptunaSharp Python Optuna 5.0 Speedup vs Optuna
TPE, 6 parameters, 500 trials 374 ms 3.13 s 8.4×
TPE multivariate, 6 parameters, 500 trials 374 ms 3.14 s 8.4×
TPE, 100 parameters, 150 trials 1.48 s 27.6 s 18.6×
GP, 6 parameters, 100 trials 3.61 s 10.3 s 2.8×
CMA-ES, 6 parameters, 3,000 trials 197 ms 4.44 s 22.5×
CMA-ES, 200 parameters, 100 trials 614 ms 1.74 s 2.8×
NSGA-II, 6 parameters, 3,000 trials 107 ms 4.13 s 38.4×
QMC, 6 parameters, 3,000 trials 39.0 ms 2.92 s 74.9×
Random, 6 parameters, 5,000 trials 66.9 ms 2.59 s 38.7×
Random, 1,000 parameters, 200 trials 316 ms 18.8 s 59.5×
Random, 4 parameters, 50,000 trials: each block of 10,000 115-143 ms 3.59-3.75 s 25.9-32.5×

The two libraries took turns on each row in the same run. The speedup is Python Optuna's time divided by OptunaSharp's, from the times before rounding (block by block for the 50,000-trial study). The Rustuna-derived optimizations of plan 13 and plan 14 behind these times keep every seeded result.

A process's first study is slower, by its start-up: .NET compiles OptunaSharp and NumSharp as they run, and Optuna imports torch, cmaes and SciPy's QMC engine when first used. In a fresh process the first 500-trial TPE study took 1.72 s in OptunaSharp and 3.19 s in Optuna, and the first GP study 5.09 s and 12.4 s. A program that runs a single short study pays that once; the tables measure the studies that follow it.

Large studies

40 float parameters in [-10, 10], seed 0, each sampler's defaults (TPE multivariate for one objective, independent for two), the workloads of Rustuna's announcement. Rustuna is the Optuna team's Rust port. OptunaSharp and Python Optuna took turns on each workload in the same run; Python's 100,000-trial studies were not run, as they take hours. Each process warmed up on the same study at a tenth of its trials.

Study OptunaSharp Python Optuna 5.0 Speedup vs Optuna
TPE, 1,000 trials 5.27 s 34.6 s 6.6×
TPE, two objectives, 1,000 trials 16.4 s 193 s 11.8×
CMA-ES, 10,000 trials 2.27 s 46.2 s 20.4×
CMA-ES, 100,000 trials 22.4 s not run -
NSGA-II, 10,000 trials 1.00 s 48.9 s 49.0×
NSGA-II, 100,000 trials 10.4 s not run -

OptunaSharp's seeded trials are Optuna's: its CMA-ES and NSGA-II studies gave the same trials bit for bit (equal hashes of every parameter and value), and its TPE trials agree to about 15 significant digits, since TPE's truncated-normal sampling rounds differently in the last bits. Part of OptunaSharp's time is the protocol it keeps so Python workers can share a study: CMA-ES pickles its state into the storage every generation, and TPE scores candidates in NumPy's order. The Performance and Compatibility guide lists the opt-in switches that trade that compatibility for speed.

For memory, new InMemoryStorage(compactFinishedTrials: true) keeps finished trials compactly and gives the same trials: the 100,000-trial CMA-ES study above keeps 256 MB instead of 1,286 MB once finished, and NSGA-II's 87 MB instead of 667 MB, and both run faster (21.8 s and 8.7 s). Finished trials then hand out read-only views instead of dictionaries; the guide lists what that changes.

Executors

Trials run on OptunaSharp's own dedicated threads by default, or on the .NET ThreadPool (TrialExecutorKind.DedicatedThreads or TrialExecutorKind.ThreadPool). The blocking rows start twice and once as many workers as the pool's minimum, which is the processor count, and time each process's first study, while its pool is still at that minimum:

Workload Dedicated threads ThreadPool
5,000 cheap Random trials, nJobs: 8 33.6k trials/s 34.8k trials/s
2,000 TPE trials, nJobs: 8 929 trials/s 930 trials/s
200 calls of Optimize(nTrials: 8, nJobs: 8) 3,819 calls/s 3,838 calls/s
80 trials blocking 1.5 s, nJobs: 40 (ideal 3.0 s) 3.01 s 6.00 s
Longest delay before other Task.Run work started, nJobs: 40 / 20 1.8 ms / 1.7 ms 1.50 s / 0.75 s

The two match on throughput. Once nJobs reaches the pool's ready threads (20 on this machine), the ThreadPool runs fewer trials at once and delays the rest of the application, so it suits an nJobs well below the core count.

Per-worker samplers

nJobs: 8: one sampler shared by every worker, taking one call at a time (SamplerConcurrency.Shared, Optuna's behaviour and the default), against one sampler per worker (SamplerConcurrency.PerWorker):

Study Shared PerWorker
TPE multivariate, 10 parameters, 600 trials, 5 ms objective 0.79 s 0.50 s
CMA-ES, 40 parameters, 2,000 trials, cheap objective 0.92 s 0.52 s
TPE, 3 parameters, 400 trials, 20 ms objective 1.04 s 1.04 s

A sampler per worker pays where sampling dominates and changes nothing when the objective does. Slow model-based samplers stay shared: a GPSampler study of 120 trials with a 20 ms objective took 7.66 s at nJobs: 1 and 11.8 s at nJobs: 8.

Install

dotnet add package OptunaSharp

The package targets .NET 8 and .NET 10. Storages, cloud artifact stores, Plotly figures, the CLI and the Python bridges are separate OptunaSharp.* packages (listed in the package README).

Quick start

using OptunaSharp;
using OptunaSharp.Samplers;

var study = Optuna.CreateStudy(sampler: new TPESampler(seed: 42));

study.Optimize(trial =>
{
    double x = trial.SuggestFloat("x", -10, 10);
    return (x - 2) * (x - 2);
}, nTrials: 20);

Console.WriteLine(study.BestValue);        // 0.0004973004749973951
Console.WriteLine(study.BestParams["x"]);  // 2.0223002348641756

Those two numbers are exactly what Python Optuna 5.0.0 prints for the same seeded study.

The NumSharp reference

OptunaSharp.Core depends on NumSharp and its native LAPACK/CBLAS backend NumSharp.Interop.OpenBLAS, both consumed as NuGet packages from nuget.org (version pinned by the NumSharpVersion property in Directory.Build.props). Build and CI restore them from nuget.org; the OpenBLAS package ships the native LAPACK binaries (in runtimes/<rid>/native/), so np.linalg computes through LAPACK and is bit-identical to NumPy - no extra setup.

The matching NumSharp source is vendored reference-only as a pinned git submodule at refs/NumSharp (the same release tag), for browsing - it is not part of the build. Restore it, if you want it, with git submodule update --init refs/NumSharp.

Build & test

# Build everything
dotnet build SciSharp.OptunaSharp.sln -c Release

# Unit tests (both TFMs), excluding known-failing/heavy/slow categories as per-push CI does
# (drop `&TestCategory!=Slow` for the full run)
dotnet test test/OptunaSharp.Tests/OptunaSharp.Tests.csproj -c Release \
  --filter "TestCategory!=OpenBugs&TestCategory!=HighMemory&TestCategory!=RequiresPython&TestCategory!=Slow"

# Live-Optuna interop: needs a discoverable Python with the vendored Optuna installed, which is what
# the parity is measured against (`git submodule update --init refs/optuna`, then
# `pip install -e refs/optuna`)
dotnet test test/OptunaSharp.Tests.Interop/OptunaSharp.Tests.Interop.csproj -c Release

# Pack the OptunaSharp package
dotnet pack src/OptunaSharp.Core/OptunaSharp.Core.csproj -c Release -o packages

Test categories (OpenBugs, Misaligned, RequiresPython, HighMemory, WindowsOnly, Slow) are typed attributes in test/OptunaSharp.Tests/TestCategory.cs, so CI filter strings stay stable as the suite grows.

Scripting with dotnet run (.NET 10 file-based apps)

Internal members are exposed to a dotnet run script that overrides its assembly name to a declared friend (see src/OptunaSharp.Core/Assembly/Properties.cs). A script under the repo root inherits Directory.Build.props and is signed automatically; one run from elsewhere must pass the signing properties itself:

#:project <path-to-your-clone>/src/OptunaSharp.Core/OptunaSharp.Core.csproj
#:property AssemblyName=OptunaSharp.DotNetRunScript
#:property PublishAot=false
#:property SignAssembly=true
#:property AssemblyOriginatorKeyFile=<path-to-your-clone>/Open.snk

License

MIT - see LICENSE. OptunaSharp is a derivative of Optuna (© Preferred Networks, Inc.) and retains its copyright notice as the MIT License requires.

Related SciSharp projects

About

A hyperparameter optimization framework for .NET

Resources

Code of conduct

Contributing

Security policy

Stars

1 star

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages