From 6f0b6c3032e90f1d41cb7bce347feb43584b7d7b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 31 Mar 2026 23:30:51 +0000 Subject: [PATCH 1/6] Initial plan From e1ff51f65978b6ba94595d3666a1bf3435dfdb31 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 31 Mar 2026 23:36:59 +0000 Subject: [PATCH 2/6] Add _tutoriels/nanobind: nanobind+OpenMP tutorial mirroring pybind11 tutorial Agent-Logs-Url: https://github.com/sdpython/teachcompute/sessions/f50e4f34-8e6d-4164-827b-8afae3c78119 Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com> --- _tutoriels/nanobind/dotnanobind.cpp | 46 +++++++++++++++++++++++++++++ _tutoriels/nanobind/setup.py | 33 +++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 _tutoriels/nanobind/dotnanobind.cpp create mode 100644 _tutoriels/nanobind/setup.py diff --git a/_tutoriels/nanobind/dotnanobind.cpp b/_tutoriels/nanobind/dotnanobind.cpp new file mode 100644 index 0000000..affbec9 --- /dev/null +++ b/_tutoriels/nanobind/dotnanobind.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +#include // <- OpenMP + +namespace nb = nanobind; + +using Array1D = nb::ndarray, nb::c_contig, nb::device::cpu>; + +nb::ndarray> add_scaled_omp(Array1D a, Array1D b) { + size_t n = a.shape(0); + const double *pa = a.data(); + const double *pb = b.data(); + + auto result = std::make_unique(n); + + #pragma omp parallel for + for (size_t i = 0; i < n; ++i) { + result[i] = pa[i] + 2.0 * pb[i]; + } + + double *raw = result.release(); + nb::capsule owner(raw, [](void *p) noexcept { delete[] (double *)p; }); + size_t shape[1] = {n}; + return nb::ndarray>(raw, 1, shape, owner); +} + +double dot_product_omp(Array1D a, Array1D b) { + size_t n = a.shape(0); + const double *pa = a.data(); + const double *pb = b.data(); + + double total = 0.0; + + #pragma omp parallel for reduction(+:total) + for (size_t i = 0; i < n; ++i) { + total += pa[i] * pb[i]; + } + + return total; +} + +NB_MODULE(dotnanobind, m) { + m.def("add_scaled", &add_scaled_omp, "Add a + 2*b using OpenMP"); + m.def("dot_product", &dot_product_omp, "Dot product using OpenMP"); +} diff --git a/_tutoriels/nanobind/setup.py b/_tutoriels/nanobind/setup.py new file mode 100644 index 0000000..6e392c7 --- /dev/null +++ b/_tutoriels/nanobind/setup.py @@ -0,0 +1,33 @@ +from pathlib import Path + +from setuptools import Extension, setup + +import nanobind + +# Compiler flags for OpenMP +extra_compile_args = ["-O3", "-fopenmp", "-std=c++17", "-fvisibility=hidden"] +extra_link_args = ["-fopenmp"] + +nb_include = nanobind.include_dir() +nb_src = Path(nanobind.source_dir()) / "nb_combined.cpp" +# nanobind bundles tsl/robin_map, needed when building nb_combined.cpp directly +nb_ext_include = str(Path(nanobind.include_dir()).parent / "ext" / "robin_map" / "include") + +ext_modules = [ + Extension( + "dotnanobind", + sources=["dotnanobind.cpp", str(nb_src)], + include_dirs=[nb_include, nb_ext_include], + extra_compile_args=extra_compile_args, + extra_link_args=extra_link_args, + ), +] + +setup( + name="dotnanobind", + version="0.1", + author="Ton Nom", + description="Nanobind + OpenMP dotnanobind", + ext_modules=ext_modules, + zip_safe=False, +) From 7a2ba79fad14fdd1eaeac8f2b8e83975f8aca53d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xavier=20Dupr=C3=A9?= Date: Wed, 1 Apr 2026 09:21:17 +0200 Subject: [PATCH 3/6] style --- _doc/examples/plot_benchmark_dot_mul_timeit.py | 1 - _doc/examples/plot_serialisation_examples.py | 2 +- _tutoriels/nanobind/setup.py | 4 +++- _unittests/ut_xrun_doc/test_documentation_examples.py | 1 - 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/_doc/examples/plot_benchmark_dot_mul_timeit.py b/_doc/examples/plot_benchmark_dot_mul_timeit.py index 68cf058..e8949b8 100644 --- a/_doc/examples/plot_benchmark_dot_mul_timeit.py +++ b/_doc/examples/plot_benchmark_dot_mul_timeit.py @@ -39,7 +39,6 @@ c_multiply_matrix_parallel_transposed as cmulparamtr, ) - va = numpy.random.randn(150, 100).astype(numpy.float64) vb = numpy.random.randn(100, 100).astype(numpy.float64) ctx = { diff --git a/_doc/examples/plot_serialisation_examples.py b/_doc/examples/plot_serialisation_examples.py index dc1efd7..3f483a8 100644 --- a/_doc/examples/plot_serialisation_examples.py +++ b/_doc/examples/plot_serialisation_examples.py @@ -15,6 +15,7 @@ Ecriture (json) +++++++++++++++ """ + from io import StringIO, BytesIO import timeit import json @@ -25,7 +26,6 @@ import matplotlib.pyplot as plt import pandas - data = { "records": [ { diff --git a/_tutoriels/nanobind/setup.py b/_tutoriels/nanobind/setup.py index 6e392c7..e7000e9 100644 --- a/_tutoriels/nanobind/setup.py +++ b/_tutoriels/nanobind/setup.py @@ -11,7 +11,9 @@ nb_include = nanobind.include_dir() nb_src = Path(nanobind.source_dir()) / "nb_combined.cpp" # nanobind bundles tsl/robin_map, needed when building nb_combined.cpp directly -nb_ext_include = str(Path(nanobind.include_dir()).parent / "ext" / "robin_map" / "include") +nb_ext_include = str( + Path(nanobind.include_dir()).parent / "ext" / "robin_map" / "include" +) ext_modules = [ Extension( diff --git a/_unittests/ut_xrun_doc/test_documentation_examples.py b/_unittests/ut_xrun_doc/test_documentation_examples.py index 4933deb..264b609 100644 --- a/_unittests/ut_xrun_doc/test_documentation_examples.py +++ b/_unittests/ut_xrun_doc/test_documentation_examples.py @@ -7,7 +7,6 @@ from teachcompute import __file__ as teachcompute_file, has_cuda from teachcompute.ext_test_case import ExtTestCase, has_transformers, is_apple - VERBOSE = 0 ROOT = os.path.realpath(os.path.abspath(os.path.join(teachcompute_file, "..", ".."))) From d7db0f06a48d28cd6834c7987f4018344e0d0d13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xavier=20Dupr=C3=A9?= Date: Wed, 1 Apr 2026 09:37:00 +0200 Subject: [PATCH 4/6] fix --- .gitignore | 3 +++ _doc/articles/2024-05-31-route2024-gpu.rst | 6 ------ _doc/conf.py | 9 ++++----- _doc/examples/plot_export_model_onnx.py | 13 ++++++++++--- requirements-dev.txt | 3 +-- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 018f937..a1e0f7c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ *.dot *.dylib +*.ep +*.ep.graph *.prof *.pyc *.pyd @@ -9,6 +11,7 @@ *.c .build_path.txt *.onnx +*.xlsx plot*.png plot*.csv plot*.xlsx diff --git a/_doc/articles/2024-05-31-route2024-gpu.rst b/_doc/articles/2024-05-31-route2024-gpu.rst index 70dc702..6b48301 100644 --- a/_doc/articles/2024-05-31-route2024-gpu.rst +++ b/_doc/articles/2024-05-31-route2024-gpu.rst @@ -28,12 +28,6 @@ Optimize a machine learning model by fusing operations. pip install -U huggingface_hub huggingface-cli download alpindale/Llama-2-7b-ONNX --repo-type model --cache-dir cache --local-dir . --local-dir-use-symlinks False -Or: - -:: - - python -m experimental_experiment.torch_bench.export_model --model llama --mixed 1 --exporter script --config small --num_hidden_layer 2 - .. image:: images/netron.png One part of the llama model: diff --git a/_doc/conf.py b/_doc/conf.py index c570bb1..0f69983 100644 --- a/_doc/conf.py +++ b/_doc/conf.py @@ -91,22 +91,21 @@ mathjax3_config = {"chtml": {"displayAlign": "left"}} intersphinx_mapping = { - "experimental_experiment": ( - "https://sdpython.github.io/doc/experimental-experiment/dev/", - None, - ), "IPython": ("https://ipython.readthedocs.io/en/stable/", None), "matplotlib": ("https://matplotlib.org/", None), "numpy": ("https://numpy.org/doc/stable", None), "onnx": ("https://onnx.ai/onnx/", None), "onnx_array_api": ("https://sdpython.github.io/doc/onnx-array-api/dev/", None), - "onnx_diagnostic": ("https://sdpython.github.io/doc/onnx-diagnostic/dev/", None), "onnx_extended": ("https://sdpython.github.io/doc/onnx-extended/dev/", None), "pandas": ("https://pandas.pydata.org/pandas-docs/stable/", None), "python": (f"https://docs.python.org/{sys.version_info.major}", None), "scipy": ("https://docs.scipy.org/doc/scipy/reference", None), "sklearn": ("https://scikit-learn.org/stable/", None), "torch": ("https://pytorch.org/docs/stable/", None), + "yobx": ( + "https://sdpython.github.io/doc/yet-another-onnx-builder/dev/", + None, + ), } # Check intersphinx reference targets exist diff --git a/_doc/examples/plot_export_model_onnx.py b/_doc/examples/plot_export_model_onnx.py index 92da717..9b0cd41 100644 --- a/_doc/examples/plot_export_model_onnx.py +++ b/_doc/examples/plot_export_model_onnx.py @@ -96,10 +96,17 @@ def generate_example_inputs(batch: int, seq: int, vocab_size: int): def export(model, args, filename, dynamic_shapes): - from experimental_experiment.torch_interpreter import to_onnx, ExportOptions - from onnx_diagnostic.torch_export_patches import bypass_export_some_errors + from yobx.torch import ( + to_onnx, + ExportOptions, + apply_patches_for_model, + register_flattening_functions, + ) - with bypass_export_some_errors(patch_transformers=True): + with ( + register_flattening_functions(patch_transformers=True), + apply_patches_for_model(patch_transformers=True, model=model), + ): to_onnx( model, args, diff --git a/requirements-dev.txt b/requirements-dev.txt index cae7cfc..b799bcf 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -7,7 +7,7 @@ coverage cython>=3.0.10 cython-lint cytoolz -git+https://github.com/sdpython/experimental-experiment.git; sys_platform != 'darwin' +git+https://github.com/sdpython/yet-another-onnx-builder.git; sys_platform != 'darwin' furo ipython joblib @@ -17,7 +17,6 @@ mypy nbsphinx numba onnx -onnx-diagnostic>=0.3.0 openpyxl pandas pillow From ce9fb03d18bb1914ebbcbcaa415580e2a2e6e9f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xavier=20Dupr=C3=A9?= Date: Wed, 1 Apr 2026 10:09:38 +0200 Subject: [PATCH 5/6] fix --- requirements-dev.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index b799bcf..a14a953 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -7,7 +7,7 @@ coverage cython>=3.0.10 cython-lint cytoolz -git+https://github.com/sdpython/yet-another-onnx-builder.git; sys_platform != 'darwin' +git+https://github.com/xadupre/yet-another-onnx-builder.git; sys_platform != 'darwin' furo ipython joblib From b3fa5a8496350252fac4ab97f0af523878431236 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xavier=20Dupr=C3=A9?= Date: Wed, 1 Apr 2026 10:22:50 +0200 Subject: [PATCH 6/6] fix --- requirements-dev.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index a14a953..5e2397d 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -17,7 +17,9 @@ mypy nbsphinx numba onnx +onnxscript openpyxl +optree pandas pillow protobuf @@ -27,7 +29,7 @@ pytest pytest-cov torchaudio torchvision -transformers>=4.49; sys_platform != 'darwin' +transformers>=5.3; sys_platform != 'darwin' ruff sphinx sphinx-gallery