koios-model-utils 1.0.0rc1__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.
@@ -0,0 +1,64 @@
1
+ name: Release
2
+
3
+ on:
4
+ push:
5
+ tags: ["v*"]
6
+
7
+ env:
8
+ FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true
9
+
10
+ jobs:
11
+ test:
12
+ runs-on: ubuntu-latest
13
+ steps:
14
+ - uses: actions/checkout@v4
15
+ - uses: actions/setup-python@v5
16
+ with:
17
+ python-version: "3.12"
18
+ - run: pip install -e ".[dev]"
19
+ - run: ruff check src/ tests/
20
+ - run: ruff format --check src/ tests/
21
+ - run: pytest
22
+
23
+ build:
24
+ needs: test
25
+ runs-on: ubuntu-latest
26
+ steps:
27
+ - uses: actions/checkout@v4
28
+ - uses: actions/setup-python@v5
29
+ with:
30
+ python-version: "3.12"
31
+ - run: pip install build
32
+ - run: python -m build
33
+ - uses: actions/upload-artifact@v4
34
+ with:
35
+ name: dist
36
+ path: dist/
37
+
38
+ publish-pypi:
39
+ needs: build
40
+ runs-on: ubuntu-latest
41
+ environment: release
42
+ permissions:
43
+ id-token: write
44
+ steps:
45
+ - uses: actions/download-artifact@v4
46
+ with:
47
+ name: dist
48
+ path: dist/
49
+ - uses: pypa/gh-action-pypi-publish@release/v1
50
+
51
+ github-release:
52
+ needs: build
53
+ runs-on: ubuntu-latest
54
+ permissions:
55
+ contents: write
56
+ steps:
57
+ - uses: actions/download-artifact@v4
58
+ with:
59
+ name: dist
60
+ path: dist/
61
+ - uses: softprops/action-gh-release@v2
62
+ with:
63
+ files: dist/*
64
+ generate_release_notes: true
@@ -0,0 +1,54 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+ *.so
6
+ .Python
7
+ build/
8
+ develop-eggs/
9
+ dist/
10
+ downloads/
11
+ eggs/
12
+ .eggs/
13
+ lib/
14
+ lib64/
15
+ parts/
16
+ sdist/
17
+ var/
18
+ wheels/
19
+ *.egg-info/
20
+ .installed.cfg
21
+ *.egg
22
+
23
+ # Virtual environments
24
+ .venv/
25
+ venv/
26
+ env/
27
+ ENV/
28
+
29
+ # IDE
30
+ .vscode/
31
+ .idea/
32
+ *.swp
33
+ *.swo
34
+ *~
35
+
36
+ # macOS
37
+ .DS_Store
38
+ **/.DS_Store
39
+
40
+ # Testing
41
+ .pytest_cache/
42
+ .coverage
43
+ htmlcov/
44
+ .tox/
45
+
46
+ # Dev tooling
47
+ uv.lock
48
+
49
+ # Environment
50
+ .env
51
+ .env.local
52
+
53
+ # Logs
54
+ *.log
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-2026 Ai-OPs, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,22 @@
1
+ .PHONY: setup test lint format clean
2
+
3
+ setup:
4
+ pip install -e ".[sb3]"
5
+
6
+ setup-minimal:
7
+ pip install -e .
8
+
9
+ test:
10
+ python3 -m pytest tests/ -v $(ARGS)
11
+
12
+ lint:
13
+ ruff check src/ tests/
14
+
15
+ format:
16
+ ruff format src/ tests/
17
+ ruff check --fix src/ tests/
18
+
19
+ clean:
20
+ rm -rf build/ dist/ *.egg-info src/*.egg-info
21
+ find . -type d -name __pycache__ -exec rm -rf {} +
22
+ find . -type f -name "*.pyc" -delete
@@ -0,0 +1,176 @@
1
+ Metadata-Version: 2.4
2
+ Name: koios-model-utils
3
+ Version: 1.0.0rc1
4
+ Summary: Utilities for exporting and annotating ONNX models for the Koios IoT platform
5
+ Project-URL: Homepage, https://www.ai-op.com
6
+ Project-URL: Documentation, https://github.com/Ai-Ops-Inc/koios-model-utils
7
+ Project-URL: Repository, https://github.com/Ai-Ops-Inc/koios-model-utils
8
+ Project-URL: Issues, https://github.com/Ai-Ops-Inc/koios-model-utils/issues
9
+ Author-email: "Ai-OPs, Inc." <support@ai-op.com>
10
+ License: MIT
11
+ License-File: LICENSE
12
+ Keywords: industrial,iot,koios,machine-learning,onnx,reinforcement-learning
13
+ Classifier: Development Status :: 3 - Alpha
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Intended Audience :: Science/Research
16
+ Classifier: License :: OSI Approved :: MIT License
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
22
+ Classifier: Typing :: Typed
23
+ Requires-Python: >=3.11
24
+ Requires-Dist: numpy
25
+ Requires-Dist: onnx>=1.14
26
+ Provides-Extra: dev
27
+ Requires-Dist: pytest>=8.0.0; extra == 'dev'
28
+ Requires-Dist: ruff>=0.4.0; extra == 'dev'
29
+ Provides-Extra: sb3
30
+ Requires-Dist: onnxruntime>=1.16; extra == 'sb3'
31
+ Requires-Dist: onnxscript; extra == 'sb3'
32
+ Requires-Dist: stable-baselines3>=2.0; extra == 'sb3'
33
+ Requires-Dist: torch; extra == 'sb3'
34
+ Description-Content-Type: text/markdown
35
+
36
+ # koios-model-utils
37
+
38
+ Utilities for exporting and annotating ONNX models for the [Koios](https://ai-op.com) IoT platform.
39
+
40
+ This package helps data scientists prepare trained ML models (especially Stable-Baselines3 reinforcement learning models) for deployment on Koios. It handles ONNX export, metadata embedding, and normalization parameter annotation so the Koios Predict Engine can run inference correctly.
41
+
42
+ ## Installation
43
+
44
+ ```bash
45
+ # Metadata embedding only (no torch/SB3 dependency)
46
+ pip install koios-model-utils
47
+
48
+ # Full SB3 export pipeline
49
+ pip install koios-model-utils[sb3]
50
+ ```
51
+
52
+ ## Quick Start
53
+
54
+ ### Embed metadata into an existing ONNX model
55
+
56
+ ```python
57
+ import onnx
58
+ from koios_model_utils import (
59
+ InputBinding, OutputBinding, TrainingMeta,
60
+ embed_koios_metadata,
61
+ )
62
+
63
+ model = onnx.load("my_model.onnx")
64
+
65
+ inputs = [
66
+ InputBinding(name="temperature", description="Tank temperature (C)"),
67
+ InputBinding(name="pressure", description="Vessel pressure (kPa)"),
68
+ ]
69
+ outputs = [
70
+ OutputBinding(
71
+ name="valve_position",
72
+ range_min=0.0, range_max=100.0,
73
+ normalization_type="symmetric",
74
+ normalization_source="custom",
75
+ custom_minimum=0.0, custom_maximum=100.0,
76
+ clamp_output=True,
77
+ ),
78
+ ]
79
+ training = TrainingMeta(
80
+ scenario_name="tank_temperature",
81
+ algorithm="PPO",
82
+ obs_depth=5,
83
+ sample_rate=1.0,
84
+ )
85
+
86
+ embed_koios_metadata(model, inputs=inputs, outputs=outputs, training=training)
87
+ onnx.save(model, "my_model_koios.onnx")
88
+ ```
89
+
90
+ ### Export an SB3 model to ONNX
91
+
92
+ ```python
93
+ from koios_model_utils import export_onnx, build_input_bindings, build_output_bindings, TrainingMeta
94
+
95
+ inputs = build_input_bindings(
96
+ ["temperature", "level", "flow_rate"],
97
+ descriptions={"temperature": "Tank temp (C)", "level": "Tank level (%)"},
98
+ )
99
+ outputs = build_output_bindings(
100
+ ["valve_position"],
101
+ action_ranges={"valve_position": (0.0, 100.0)},
102
+ )
103
+
104
+ export_onnx(
105
+ "runs/best_model.zip",
106
+ inputs=inputs,
107
+ outputs=outputs,
108
+ training=TrainingMeta(scenario_name="tank_temperature", algorithm="PPO", obs_depth=5),
109
+ )
110
+ ```
111
+
112
+ ### CLI
113
+
114
+ ```bash
115
+ # Export with auto-detected settings
116
+ koios-export runs/best_model.zip
117
+
118
+ # Specify output path and opset version
119
+ koios-export runs/best_model.zip --output model.onnx --opset 18
120
+ ```
121
+
122
+ ## SB3 Training Callbacks
123
+
124
+ ```python
125
+ from koios_model_utils import ObsRangeCallback, VecNormalizeSyncCallback
126
+
127
+ # Track observation ranges during training
128
+ obs_range_cb = ObsRangeCallback(run_dir, verbose=1)
129
+
130
+ # Sync VecNormalize stats with best model checkpoints
131
+ vec_sync_cb = VecNormalizeSyncCallback(eval_callback, verbose=1)
132
+ ```
133
+
134
+ ## API Reference
135
+
136
+ ### Metadata Types
137
+
138
+ | Class | Description |
139
+ |-------|-------------|
140
+ | `InputBinding` | Describes one input (observation) binding — name, normalization, failure bounds |
141
+ | `OutputBinding` | Describes one output (action) binding — name, range, normalization, clamping |
142
+ | `TrainingMeta` | Model-level metadata — algorithm, obs_depth, sample_rate, model_type |
143
+
144
+ ### Functions
145
+
146
+ | Function | Description |
147
+ |----------|-------------|
148
+ | `embed_koios_metadata()` | Embed `koios.training` and `koios.bindings` metadata into an ONNX model |
149
+ | `build_input_bindings()` | Build input bindings from node names + optional normalization stats |
150
+ | `build_output_bindings()` | Build output bindings from action names + ranges |
151
+ | `export_onnx()` | Full SB3-to-ONNX export pipeline (requires `[sb3]` extra) |
152
+ | `detect_algorithm()` | Detect SB3 algorithm (PPO, SAC, TD3, etc.) from a saved `.zip` model |
153
+
154
+ ### Callbacks (requires `[sb3]` extra)
155
+
156
+ | Callback | Description |
157
+ |----------|-------------|
158
+ | `ObsRangeCallback` | Track per-feature min/max of observations during training |
159
+ | `VecNormalizeSyncCallback` | Save VecNormalize stats alongside best model checkpoints |
160
+
161
+ ## Development
162
+
163
+ ```bash
164
+ # Install with all dependencies
165
+ pip install -e ".[sb3,dev]"
166
+
167
+ # Run tests
168
+ make test
169
+
170
+ # Lint
171
+ make lint
172
+ ```
173
+
174
+ ## License
175
+
176
+ MIT License - see [LICENSE](LICENSE) for details.
@@ -0,0 +1,141 @@
1
+ # koios-model-utils
2
+
3
+ Utilities for exporting and annotating ONNX models for the [Koios](https://ai-op.com) IoT platform.
4
+
5
+ This package helps data scientists prepare trained ML models (especially Stable-Baselines3 reinforcement learning models) for deployment on Koios. It handles ONNX export, metadata embedding, and normalization parameter annotation so the Koios Predict Engine can run inference correctly.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ # Metadata embedding only (no torch/SB3 dependency)
11
+ pip install koios-model-utils
12
+
13
+ # Full SB3 export pipeline
14
+ pip install koios-model-utils[sb3]
15
+ ```
16
+
17
+ ## Quick Start
18
+
19
+ ### Embed metadata into an existing ONNX model
20
+
21
+ ```python
22
+ import onnx
23
+ from koios_model_utils import (
24
+ InputBinding, OutputBinding, TrainingMeta,
25
+ embed_koios_metadata,
26
+ )
27
+
28
+ model = onnx.load("my_model.onnx")
29
+
30
+ inputs = [
31
+ InputBinding(name="temperature", description="Tank temperature (C)"),
32
+ InputBinding(name="pressure", description="Vessel pressure (kPa)"),
33
+ ]
34
+ outputs = [
35
+ OutputBinding(
36
+ name="valve_position",
37
+ range_min=0.0, range_max=100.0,
38
+ normalization_type="symmetric",
39
+ normalization_source="custom",
40
+ custom_minimum=0.0, custom_maximum=100.0,
41
+ clamp_output=True,
42
+ ),
43
+ ]
44
+ training = TrainingMeta(
45
+ scenario_name="tank_temperature",
46
+ algorithm="PPO",
47
+ obs_depth=5,
48
+ sample_rate=1.0,
49
+ )
50
+
51
+ embed_koios_metadata(model, inputs=inputs, outputs=outputs, training=training)
52
+ onnx.save(model, "my_model_koios.onnx")
53
+ ```
54
+
55
+ ### Export an SB3 model to ONNX
56
+
57
+ ```python
58
+ from koios_model_utils import export_onnx, build_input_bindings, build_output_bindings, TrainingMeta
59
+
60
+ inputs = build_input_bindings(
61
+ ["temperature", "level", "flow_rate"],
62
+ descriptions={"temperature": "Tank temp (C)", "level": "Tank level (%)"},
63
+ )
64
+ outputs = build_output_bindings(
65
+ ["valve_position"],
66
+ action_ranges={"valve_position": (0.0, 100.0)},
67
+ )
68
+
69
+ export_onnx(
70
+ "runs/best_model.zip",
71
+ inputs=inputs,
72
+ outputs=outputs,
73
+ training=TrainingMeta(scenario_name="tank_temperature", algorithm="PPO", obs_depth=5),
74
+ )
75
+ ```
76
+
77
+ ### CLI
78
+
79
+ ```bash
80
+ # Export with auto-detected settings
81
+ koios-export runs/best_model.zip
82
+
83
+ # Specify output path and opset version
84
+ koios-export runs/best_model.zip --output model.onnx --opset 18
85
+ ```
86
+
87
+ ## SB3 Training Callbacks
88
+
89
+ ```python
90
+ from koios_model_utils import ObsRangeCallback, VecNormalizeSyncCallback
91
+
92
+ # Track observation ranges during training
93
+ obs_range_cb = ObsRangeCallback(run_dir, verbose=1)
94
+
95
+ # Sync VecNormalize stats with best model checkpoints
96
+ vec_sync_cb = VecNormalizeSyncCallback(eval_callback, verbose=1)
97
+ ```
98
+
99
+ ## API Reference
100
+
101
+ ### Metadata Types
102
+
103
+ | Class | Description |
104
+ |-------|-------------|
105
+ | `InputBinding` | Describes one input (observation) binding — name, normalization, failure bounds |
106
+ | `OutputBinding` | Describes one output (action) binding — name, range, normalization, clamping |
107
+ | `TrainingMeta` | Model-level metadata — algorithm, obs_depth, sample_rate, model_type |
108
+
109
+ ### Functions
110
+
111
+ | Function | Description |
112
+ |----------|-------------|
113
+ | `embed_koios_metadata()` | Embed `koios.training` and `koios.bindings` metadata into an ONNX model |
114
+ | `build_input_bindings()` | Build input bindings from node names + optional normalization stats |
115
+ | `build_output_bindings()` | Build output bindings from action names + ranges |
116
+ | `export_onnx()` | Full SB3-to-ONNX export pipeline (requires `[sb3]` extra) |
117
+ | `detect_algorithm()` | Detect SB3 algorithm (PPO, SAC, TD3, etc.) from a saved `.zip` model |
118
+
119
+ ### Callbacks (requires `[sb3]` extra)
120
+
121
+ | Callback | Description |
122
+ |----------|-------------|
123
+ | `ObsRangeCallback` | Track per-feature min/max of observations during training |
124
+ | `VecNormalizeSyncCallback` | Save VecNormalize stats alongside best model checkpoints |
125
+
126
+ ## Development
127
+
128
+ ```bash
129
+ # Install with all dependencies
130
+ pip install -e ".[sb3,dev]"
131
+
132
+ # Run tests
133
+ make test
134
+
135
+ # Lint
136
+ make lint
137
+ ```
138
+
139
+ ## License
140
+
141
+ MIT License - see [LICENSE](LICENSE) for details.
@@ -0,0 +1,69 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "koios-model-utils"
7
+ version = "1.0.0rc1"
8
+ description = "Utilities for exporting and annotating ONNX models for the Koios IoT platform"
9
+ readme = "README.md"
10
+ requires-python = ">=3.11"
11
+ license = {text = "MIT"}
12
+ authors = [
13
+ {name = "Ai-OPs, Inc.", email = "support@ai-op.com"}
14
+ ]
15
+ keywords = ["koios", "onnx", "iot", "industrial", "machine-learning", "reinforcement-learning"]
16
+ classifiers = [
17
+ "Development Status :: 3 - Alpha",
18
+ "Intended Audience :: Developers",
19
+ "Intended Audience :: Science/Research",
20
+ "License :: OSI Approved :: MIT License",
21
+ "Programming Language :: Python :: 3",
22
+ "Programming Language :: Python :: 3.11",
23
+ "Programming Language :: Python :: 3.12",
24
+ "Programming Language :: Python :: 3.13",
25
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
26
+ "Typing :: Typed",
27
+ ]
28
+
29
+ dependencies = [
30
+ "numpy",
31
+ "onnx>=1.14",
32
+ ]
33
+
34
+ [project.optional-dependencies]
35
+ sb3 = [
36
+ "stable-baselines3>=2.0",
37
+ "torch",
38
+ "onnxruntime>=1.16",
39
+ "onnxscript",
40
+ ]
41
+ dev = [
42
+ "pytest>=8.0.0",
43
+ "ruff>=0.4.0",
44
+ ]
45
+
46
+ [project.scripts]
47
+ koios-export = "koios_model_utils.export:main"
48
+
49
+ [project.urls]
50
+ Homepage = "https://www.ai-op.com"
51
+ Documentation = "https://github.com/Ai-Ops-Inc/koios-model-utils"
52
+ Repository = "https://github.com/Ai-Ops-Inc/koios-model-utils"
53
+ Issues = "https://github.com/Ai-Ops-Inc/koios-model-utils/issues"
54
+
55
+ [tool.hatch.build.targets.wheel]
56
+ packages = ["src/koios_model_utils"]
57
+
58
+ [tool.ruff]
59
+ line-length = 100
60
+ target-version = "py311"
61
+
62
+ [tool.ruff.lint]
63
+ select = ["E", "F", "I", "N", "W", "UP"]
64
+ ignore = ["E501"]
65
+
66
+ [tool.pytest.ini_options]
67
+ testpaths = ["tests"]
68
+ pythonpath = ["src"]
69
+ addopts = ["-v", "--tb=short"]
@@ -0,0 +1,47 @@
1
+ """koios-model-utils — utilities for exporting and annotating ONNX models for the Koios IoT platform."""
2
+
3
+ __version__ = "1.0.0rc1"
4
+
5
+ from koios_model_utils.metadata import (
6
+ InputBinding,
7
+ OutputBinding,
8
+ TrainingMeta,
9
+ build_input_bindings,
10
+ build_output_bindings,
11
+ embed_koios_metadata,
12
+ )
13
+
14
+ # SB3-dependent exports — only available with koios-model-utils[sb3]
15
+ try:
16
+ from koios_model_utils.callbacks import (
17
+ ObsRangeCallback,
18
+ VecNormalizeSyncCallback,
19
+ )
20
+ from koios_model_utils.export import (
21
+ OnnxActorWrapper,
22
+ OnnxDQNWrapper,
23
+ detect_algorithm,
24
+ export_onnx,
25
+ load_vec_normalize_stats,
26
+ )
27
+ except ImportError:
28
+ pass
29
+
30
+ __all__ = [
31
+ # metadata (always available)
32
+ "InputBinding",
33
+ "OutputBinding",
34
+ "TrainingMeta",
35
+ "embed_koios_metadata",
36
+ "build_input_bindings",
37
+ "build_output_bindings",
38
+ # export (requires sb3 extra)
39
+ "export_onnx",
40
+ "detect_algorithm",
41
+ "load_vec_normalize_stats",
42
+ "OnnxActorWrapper",
43
+ "OnnxDQNWrapper",
44
+ # callbacks (requires sb3 extra)
45
+ "ObsRangeCallback",
46
+ "VecNormalizeSyncCallback",
47
+ ]