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_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_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/_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/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..e7000e9 --- /dev/null +++ b/_tutoriels/nanobind/setup.py @@ -0,0 +1,35 @@ +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, +) 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, "..", ".."))) diff --git a/requirements-dev.txt b/requirements-dev.txt index cae7cfc..5e2397d 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/xadupre/yet-another-onnx-builder.git; sys_platform != 'darwin' furo ipython joblib @@ -17,8 +17,9 @@ mypy nbsphinx numba onnx -onnx-diagnostic>=0.3.0 +onnxscript openpyxl +optree pandas pillow protobuf @@ -28,7 +29,7 @@ pytest pytest-cov torchaudio torchvision -transformers>=4.49; sys_platform != 'darwin' +transformers>=5.3; sys_platform != 'darwin' ruff sphinx sphinx-gallery