onnx2fmu 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. onnx2fmu-0.0.1/.gitattributes +17 -0
  2. onnx2fmu-0.0.1/.github/workflows/buil_branches.yml +89 -0
  3. onnx2fmu-0.0.1/.github/workflows/build.yml +86 -0
  4. onnx2fmu-0.0.1/.gitignore +15 -0
  5. onnx2fmu-0.0.1/PKG-INFO +115 -0
  6. onnx2fmu-0.0.1/README.md +99 -0
  7. onnx2fmu-0.0.1/environment.yml +18 -0
  8. onnx2fmu-0.0.1/onnx2fmu/CMakeLists.txt +362 -0
  9. onnx2fmu-0.0.1/onnx2fmu/__init__.py +0 -0
  10. onnx2fmu-0.0.1/onnx2fmu/aarch64-linux-toolchain.cmake +3 -0
  11. onnx2fmu-0.0.1/onnx2fmu/app.py +507 -0
  12. onnx2fmu-0.0.1/onnx2fmu/include/FMI.h +209 -0
  13. onnx2fmu-0.0.1/onnx2fmu/include/FMI1.h +123 -0
  14. onnx2fmu-0.0.1/onnx2fmu/include/FMI2.h +221 -0
  15. onnx2fmu-0.0.1/onnx2fmu/include/FMI3.h +562 -0
  16. onnx2fmu-0.0.1/onnx2fmu/include/cosimulation.h +5 -0
  17. onnx2fmu-0.0.1/onnx2fmu/include/fmi1Functions.h +120 -0
  18. onnx2fmu-0.0.1/onnx2fmu/include/fmi2FunctionTypes.h +271 -0
  19. onnx2fmu-0.0.1/onnx2fmu/include/fmi2Functions.h +327 -0
  20. onnx2fmu-0.0.1/onnx2fmu/include/fmi2TypesPlatform.h +108 -0
  21. onnx2fmu-0.0.1/onnx2fmu/include/fmi3FunctionTypes.h +638 -0
  22. onnx2fmu-0.0.1/onnx2fmu/include/fmi3Functions.h +343 -0
  23. onnx2fmu-0.0.1/onnx2fmu/include/fmi3PlatformTypes.h +94 -0
  24. onnx2fmu-0.0.1/onnx2fmu/include/fmiFunctions.h +235 -0
  25. onnx2fmu-0.0.1/onnx2fmu/include/fmiModelFunctions.h +214 -0
  26. onnx2fmu-0.0.1/onnx2fmu/include/fmiModelTypes.h +91 -0
  27. onnx2fmu-0.0.1/onnx2fmu/include/fmiPlatformTypes.h +73 -0
  28. onnx2fmu-0.0.1/onnx2fmu/include/model.h +257 -0
  29. onnx2fmu-0.0.1/onnx2fmu/include/ortUtils.h +18 -0
  30. onnx2fmu-0.0.1/onnx2fmu/include/structured_variable_name.tab.h +71 -0
  31. onnx2fmu-0.0.1/onnx2fmu/src/FMI.c +419 -0
  32. onnx2fmu-0.0.1/onnx2fmu/src/FMI1.c +557 -0
  33. onnx2fmu-0.0.1/onnx2fmu/src/FMI2.c +612 -0
  34. onnx2fmu-0.0.1/onnx2fmu/src/FMI3.c +1192 -0
  35. onnx2fmu-0.0.1/onnx2fmu/src/README.md +7 -0
  36. onnx2fmu-0.0.1/onnx2fmu/src/cosimulation.c +704 -0
  37. onnx2fmu-0.0.1/onnx2fmu/src/fmi1Functions.c +644 -0
  38. onnx2fmu-0.0.1/onnx2fmu/src/fmi2Functions.c +940 -0
  39. onnx2fmu-0.0.1/onnx2fmu/src/fmi3Functions.c +1446 -0
  40. onnx2fmu-0.0.1/onnx2fmu/src/ortUtils.c +185 -0
  41. onnx2fmu-0.0.1/onnx2fmu/src/structured_variable_name.l +41 -0
  42. onnx2fmu-0.0.1/onnx2fmu/src/structured_variable_name.tab.c +1547 -0
  43. onnx2fmu-0.0.1/onnx2fmu/src/structured_variable_name.y +55 -0
  44. onnx2fmu-0.0.1/onnx2fmu/src/structured_variable_name.yy.c +1826 -0
  45. onnx2fmu-0.0.1/onnx2fmu/template/FMI1CS.xml +29 -0
  46. onnx2fmu-0.0.1/onnx2fmu/template/FMI2.xml +66 -0
  47. onnx2fmu-0.0.1/onnx2fmu/template/FMI3.xml +33 -0
  48. onnx2fmu-0.0.1/onnx2fmu/template/buildDescription.xml +13 -0
  49. onnx2fmu-0.0.1/onnx2fmu/template/config.h +55 -0
  50. onnx2fmu-0.0.1/onnx2fmu/template/model.c +287 -0
  51. onnx2fmu-0.0.1/onnx2fmu/template/resources/y.txt +1 -0
  52. onnx2fmu-0.0.1/pyproject.toml +41 -0
  53. onnx2fmu-0.0.1/tests/example1/example1.onnx +0 -0
  54. onnx2fmu-0.0.1/tests/example1/example1Description.json +37 -0
  55. onnx2fmu-0.0.1/tests/example1/generate-example-model.ipynb +302 -0
  56. onnx2fmu-0.0.1/tests/example1/input.csv +101 -0
  57. onnx2fmu-0.0.1/tests/example1/output.csv +101 -0
  58. onnx2fmu-0.0.1/tests/example2/example2.onnx +142 -0
  59. onnx2fmu-0.0.1/tests/example2/example2Description.json +25 -0
  60. onnx2fmu-0.0.1/tests/example2/generate-example-model.ipynb +257 -0
  61. onnx2fmu-0.0.1/tests/example2/input.csv +101 -0
  62. onnx2fmu-0.0.1/tests/example2/output.csv +101 -0
  63. onnx2fmu-0.0.1/tests/test_app.py +164 -0
  64. onnx2fmu-0.0.1/version.txt +2 -0
@@ -0,0 +1,17 @@
1
+ # Set the default behavior, in case people don't have core.autocrlf set.
2
+ * text=auto
3
+
4
+ # Explicitly declare text files we want to always be normalized and converted
5
+ # to native line endings on checkout.
6
+ *.c text
7
+ *.csv text
8
+ *.h text
9
+ *.html text
10
+ *.md text
11
+ *.py text
12
+ *.txt text
13
+ *.yml text
14
+ .gitattributes text
15
+ .gitignore text
16
+ .mailmap text
17
+ LICENSE text
@@ -0,0 +1,89 @@
1
+ name: Build and test the project using self-hosted runners
2
+
3
+ on:
4
+ push:
5
+ branches-ignore:
6
+ - master
7
+ pull_request:
8
+ branches-ignore:
9
+ - master
10
+
11
+ jobs:
12
+
13
+ build_and_test_windows:
14
+ strategy:
15
+ matrix:
16
+ arch: [x86_64]
17
+ os: [windows]
18
+ python-version: ["3.10", "3.11", "3.12"]
19
+ runs-on: [self-hosted, windows]
20
+
21
+ steps:
22
+ - name: Check out repository
23
+ uses: actions/checkout@v4
24
+
25
+ - name: Setup Python ${{ matrix.python-version }}
26
+ uses: actions/setup-python@v5
27
+ with:
28
+ python-version: ${{ matrix.python-version }}
29
+ cache: "pip"
30
+
31
+ - name: Install dependencies
32
+ run: |
33
+ python -m pip install --upgrade pip
34
+ pip install numpy pandas onnx jinja2 loguru typer fmpy cmake
35
+ pip install -e .
36
+
37
+ - name: Python unit tests
38
+ run: |
39
+ python -m unittest tests.test_app
40
+
41
+ - name: Build example1 FMU
42
+ shell: pwsh
43
+ run: |
44
+ $fmi_platform = "${{ matrix.arch }}-${{ matrix.os }}"
45
+ onnx2fmu build tests/example1/example1.onnx tests/example1/example1Description.json --fmi-platform $fmi_platform
46
+
47
+ - name: Build example2 FMU
48
+ shell: pwsh
49
+ run: |
50
+ $fmi_platform = "${{ matrix.arch }}-${{ matrix.os }}"
51
+ onnx2fmu build tests/example2/example2.onnx tests/example2/example2Description.json --fmi-platform $fmi_platform
52
+
53
+ build_and_test_linux:
54
+ strategy:
55
+ matrix:
56
+ arch: [x86_64]
57
+ os: [linux]
58
+ python-version: ["3.10", "3.11", "3.12"]
59
+ runs-on: [self-hosted, linux]
60
+
61
+ steps:
62
+ - name: Check out repository
63
+ uses: actions/checkout@v4
64
+
65
+ - name: Setup Python ${{ matrix.python-version }}
66
+ uses: actions/setup-python@v5
67
+ with:
68
+ python-version: ${{ matrix.python-version }}
69
+ cache: "pip"
70
+
71
+ - name: Install dependencies
72
+ run: |
73
+ python -m pip install --upgrade pip
74
+ pip install numpy pandas onnx jinja2 loguru typer fmpy cmake
75
+ pip install -e .
76
+
77
+ - name: Python unit tests
78
+ run: |
79
+ python -m unittest tests.test_app
80
+
81
+ - name: Build example1 FMU
82
+ shell: bash -l {0}
83
+ run: |
84
+ onnx2fmu build tests/example1/example1.onnx tests/example1/example1Description.json --fmi-platform ${{ matrix.arch }}-${{ matrix.os }}
85
+
86
+ - name: Build example2 FMU
87
+ shell: bash -l {0}
88
+ run: |
89
+ onnx2fmu build tests/example2/example2.onnx tests/example2/example2Description.json --fmi-platform ${{ matrix.arch }}-${{ matrix.os }}
@@ -0,0 +1,86 @@
1
+ name: Build and test the project master branch
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - master
7
+ pull_request:
8
+ branches:
9
+ - master
10
+
11
+ jobs:
12
+
13
+ build_and_test:
14
+ strategy:
15
+ matrix:
16
+ include:
17
+ - name: x86-windows
18
+ image: windows-2022
19
+ arch: x86
20
+ python-version: "3.10"
21
+ - name: x86_64-windows
22
+ image: windows-2022
23
+ arch: x86_64
24
+ python-version: "3.10"
25
+ - name: x86_64-linux
26
+ image: ubuntu-20.04
27
+ arch: x86_64
28
+ python-version: "3.10"
29
+ - name: aarch64-linux
30
+ image: ubuntu-20.04
31
+ arch: aarch64
32
+ python-version: "3.10"
33
+ # python-version: ["3.10"]
34
+ # - name: x86_64-darwin
35
+ # image: macos-13
36
+ # arch: x86_64
37
+ # python-version: ["3.10"]
38
+ # - name: aarch64-darwin
39
+ # image: macos-13
40
+ # arch: aarch64
41
+ # python-version: ["3.10"]
42
+ runs-on: ${{ matrix.image }}
43
+
44
+ steps:
45
+ - name: Check out repository
46
+ uses: actions/checkout@v4
47
+
48
+ - name: Setup Python ${{ matrix.python-version }}
49
+ uses: actions/setup-python@v5
50
+ with:
51
+ python-version: ${{ matrix.python-version }}
52
+ cache: "pip"
53
+
54
+ - name: Install dependencies
55
+ run: |
56
+ python -m pip install --upgrade pip
57
+ pip install numpy pandas onnx jinja2 loguru typer fmpy cmake
58
+ pip install -e .
59
+
60
+ - name: Python unit tests
61
+ run: |
62
+ python -m unittest tests.test_app
63
+
64
+ - name: Build example1 FMU (Windows)
65
+ if: runner.os == 'Windows'
66
+ shell: cmd
67
+ run: |
68
+ onnx2fmu build tests/example1/example1.onnx tests/example1/example1Description.json --fmi-platform ${{ matrix.name }}
69
+
70
+ - name: Build example1 FMU (non-Windows)
71
+ if: runner.os != 'Windows'
72
+ shell: bash
73
+ run: |
74
+ onnx2fmu build tests/example1/example1.onnx tests/example1/example1Description.json --fmi-platform ${{ matrix.name }}
75
+
76
+ - name: Build example2 FMU (Windows)
77
+ if: runner.os == 'Windows'
78
+ shell: cmd
79
+ run: |
80
+ onnx2fmu build tests/example2/example2.onnx tests/example2/example2Description.json --fmi-platform ${{ matrix.name }}
81
+
82
+ - name: Build example2 FMU (non-Windows)
83
+ if: runner.os != 'Windows'
84
+ shell: bash
85
+ run: |
86
+ onnx2fmu build tests/example2/example2.onnx tests/example2/example2Description.json --fmi-platform ${{ matrix.name }}
@@ -0,0 +1,15 @@
1
+ # build artifacts
2
+ __pycache__
3
+ .cache
4
+ .idea/
5
+ *.fmu
6
+ *.zip
7
+ *_out.csv
8
+
9
+ # Environment variables
10
+ .env
11
+
12
+ # Python virtual environments
13
+ .venv/
14
+ /build/
15
+ .vscode/
@@ -0,0 +1,115 @@
1
+ Metadata-Version: 2.4
2
+ Name: onnx2fmu
3
+ Version: 0.0.1
4
+ Summary: A tool for wrapping ONNX netwroks in FMUs.
5
+ Author-email: Michele Urbani <murbani@fbk.eu>
6
+ Requires-Python: ~=3.10
7
+ Description-Content-Type: text/markdown
8
+ Classifier: Programming Language :: Python :: 3
9
+ Requires-Dist: onnx
10
+ Requires-Dist: jinja2
11
+ Requires-Dist: cmake
12
+ Requires-Dist: numpy
13
+ Requires-Dist: loguru
14
+ Requires-Dist: typer
15
+
16
+ # ONNX2FMU: Encapsulate ONNX models in Functional Mock-up Units (FMUs)
17
+
18
+ **What ONNX2FMU does?** It wraps ONNX models into FMUs by generating the C code
19
+ and subsequently compiling it.
20
+
21
+ ## 🚀 Get started
22
+
23
+ - Python 3.10+
24
+ - CMake 3.22+
25
+ - A code compiler for the host platform, which could be one of linux, windows
26
+ and macos.
27
+
28
+ The default Windows CMake Generator is Visual Studio 2022.
29
+
30
+ ## 📝 ONNX model declaration
31
+
32
+ ONNX2FMU can handle models with multiple inputs and outputs as far as
33
+ 1. the model description lists all inputs and outputs, and node names are
34
+ compliant with the node names in the ONNX model;
35
+ 2. Input and output nodes have no 0 dimension, i.e., they have shapes like
36
+ `(N, )`, `(1, N)`, etc., with `N` being any tensor dimension.
37
+
38
+ ### Model description file
39
+
40
+ A model description is declared in a JSON file and its schema includes the
41
+ following global items:
42
+
43
+ - `"name"` is the model name, which will also be the FMU archive name,
44
+ - `"description"` provides a generic description of the model,
45
+ - `"FMIVersion"` is the FMI standard version for generatign the FMU code and
46
+ the FMU binaries,
47
+ - `"inputs"` and `"outputs"` are the lists of inputs and output nodes in the
48
+ ONNX model.
49
+
50
+ Each entry of the the inputs and output lists is characterized by the following
51
+ schema:
52
+
53
+ - `"name"` must match the name of one of the model nodes, whereas
54
+ - `"names"` is the list of user-provided names for each of the node elements.
55
+ The number of names must match the number of elements in a given entry.
56
+ - `"description"` allows the user to attach a description to each of the
57
+ arrays.
58
+
59
+ The following is an example of a model description for a model with three
60
+ input nodes and one output node.
61
+
62
+ ### Causality of model variables
63
+
64
+ Variables of type `local` can be declared but they do not change during a
65
+ simulation. Indeed, the value of `local` variables must be defined *ad hoc* in
66
+ the model and cannot be defined automatically by ONNX2FMU.
67
+
68
+ ### State variables
69
+
70
+ State variables, which may usually be declare using variables with `local`
71
+ causality, are not handled by ONNX2FMU. This is because there is no standard
72
+ behaviour to rely on for their definition in the source code.
73
+
74
+ To handle state variables, the original ONNX model should be wrapped in model
75
+ that keep track of the state. The following example could help understanding
76
+ how to manage the presence of a state.
77
+
78
+ ```python
79
+ class CompleteModel(nn.Module):
80
+
81
+ def __init__(self):
82
+ super(CompleteModel).__init__()
83
+ self.fc1 = ...
84
+ self.relu = ...
85
+
86
+ def forward(self, X):
87
+ X = self.fc1(X)
88
+ X = self.relu(X)
89
+ return X
90
+
91
+
92
+ class WrapperModel(nn.Module):
93
+
94
+ def __init__(self, complete_model: CompleteModel, initial_state):
95
+ super(WrapperModel).__init__()
96
+ self.model = complete_model
97
+ self.state = initial_state
98
+
99
+ def forward(self, u):
100
+ # Concatenate the control u with the state
101
+ X = torch.stack((u, self.state))
102
+ # Predic the state of the system at time t
103
+ Y = self.model(X)
104
+ # Part of the output Y is the state of the system at time t, which is
105
+ # stored in self.state
106
+ self.state = Y[:(N_state_elements)]
107
+ # Return the complete output to be used in the rest of the simulation
108
+ return Y
109
+
110
+ ```
111
+
112
+ ## ONNX model generation
113
+
114
+ **Only co-simulation** FMUs can be generated.
115
+
@@ -0,0 +1,99 @@
1
+ # ONNX2FMU: Encapsulate ONNX models in Functional Mock-up Units (FMUs)
2
+
3
+ **What ONNX2FMU does?** It wraps ONNX models into FMUs by generating the C code
4
+ and subsequently compiling it.
5
+
6
+ ## 🚀 Get started
7
+
8
+ - Python 3.10+
9
+ - CMake 3.22+
10
+ - A code compiler for the host platform, which could be one of linux, windows
11
+ and macos.
12
+
13
+ The default Windows CMake Generator is Visual Studio 2022.
14
+
15
+ ## 📝 ONNX model declaration
16
+
17
+ ONNX2FMU can handle models with multiple inputs and outputs as far as
18
+ 1. the model description lists all inputs and outputs, and node names are
19
+ compliant with the node names in the ONNX model;
20
+ 2. Input and output nodes have no 0 dimension, i.e., they have shapes like
21
+ `(N, )`, `(1, N)`, etc., with `N` being any tensor dimension.
22
+
23
+ ### Model description file
24
+
25
+ A model description is declared in a JSON file and its schema includes the
26
+ following global items:
27
+
28
+ - `"name"` is the model name, which will also be the FMU archive name,
29
+ - `"description"` provides a generic description of the model,
30
+ - `"FMIVersion"` is the FMI standard version for generatign the FMU code and
31
+ the FMU binaries,
32
+ - `"inputs"` and `"outputs"` are the lists of inputs and output nodes in the
33
+ ONNX model.
34
+
35
+ Each entry of the the inputs and output lists is characterized by the following
36
+ schema:
37
+
38
+ - `"name"` must match the name of one of the model nodes, whereas
39
+ - `"names"` is the list of user-provided names for each of the node elements.
40
+ The number of names must match the number of elements in a given entry.
41
+ - `"description"` allows the user to attach a description to each of the
42
+ arrays.
43
+
44
+ The following is an example of a model description for a model with three
45
+ input nodes and one output node.
46
+
47
+ ### Causality of model variables
48
+
49
+ Variables of type `local` can be declared but they do not change during a
50
+ simulation. Indeed, the value of `local` variables must be defined *ad hoc* in
51
+ the model and cannot be defined automatically by ONNX2FMU.
52
+
53
+ ### State variables
54
+
55
+ State variables, which may usually be declare using variables with `local`
56
+ causality, are not handled by ONNX2FMU. This is because there is no standard
57
+ behaviour to rely on for their definition in the source code.
58
+
59
+ To handle state variables, the original ONNX model should be wrapped in model
60
+ that keep track of the state. The following example could help understanding
61
+ how to manage the presence of a state.
62
+
63
+ ```python
64
+ class CompleteModel(nn.Module):
65
+
66
+ def __init__(self):
67
+ super(CompleteModel).__init__()
68
+ self.fc1 = ...
69
+ self.relu = ...
70
+
71
+ def forward(self, X):
72
+ X = self.fc1(X)
73
+ X = self.relu(X)
74
+ return X
75
+
76
+
77
+ class WrapperModel(nn.Module):
78
+
79
+ def __init__(self, complete_model: CompleteModel, initial_state):
80
+ super(WrapperModel).__init__()
81
+ self.model = complete_model
82
+ self.state = initial_state
83
+
84
+ def forward(self, u):
85
+ # Concatenate the control u with the state
86
+ X = torch.stack((u, self.state))
87
+ # Predic the state of the system at time t
88
+ Y = self.model(X)
89
+ # Part of the output Y is the state of the system at time t, which is
90
+ # stored in self.state
91
+ self.state = Y[:(N_state_elements)]
92
+ # Return the complete output to be used in the rest of the simulation
93
+ return Y
94
+
95
+ ```
96
+
97
+ ## ONNX model generation
98
+
99
+ **Only co-simulation** FMUs can be generated.
@@ -0,0 +1,18 @@
1
+ name: onnx2fmu
2
+ channels:
3
+ - conda-forge
4
+ - defaults
5
+ dependencies:
6
+ - python=3.10
7
+ - pip
8
+ - onnx
9
+ - jinja2
10
+ - loguru
11
+ - typer
12
+ - flake8
13
+ - fmpy
14
+ - cmake
15
+ - numpy
16
+ - pandas
17
+ - pip:
18
+ - -e .