tinymlc 0.1.0__py3-none-any.whl

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 (47) hide show
  1. TinyMLC/ANG/__init__.py +0 -0
  2. TinyMLC/ANG/args.py +86 -0
  3. TinyMLC/ANG/estimator.py +103 -0
  4. TinyMLC/ANG/estimator_hal.py +184 -0
  5. TinyMLC/ANG/estimator_qemu.py +257 -0
  6. TinyMLC/ANG/estimator_software.py +130 -0
  7. TinyMLC/ANG/model_builder.py +508 -0
  8. TinyMLC/ANG/model_generator.py +439 -0
  9. TinyMLC/ANG/model_info.py +283 -0
  10. TinyMLC/ANG/utils.py +420 -0
  11. TinyMLC/__init__.py +0 -0
  12. TinyMLC/cli.py +126 -0
  13. TinyMLC/codegen.py +877 -0
  14. TinyMLC/converter/__init__.py +0 -0
  15. TinyMLC/converter/export_weights.py +382 -0
  16. TinyMLC/converter/parser_litert.py +757 -0
  17. TinyMLC/converter/parser_onnx.py +649 -0
  18. TinyMLC/generate_lut.py +97 -0
  19. TinyMLC/handlers.py +325 -0
  20. TinyMLC/ops.py +76 -0
  21. TinyMLC/templates/lut.c.tpl +23 -0
  22. TinyMLC/templates/lut.h.tpl +67 -0
  23. TinyMLC/templates/model.c.tpl +314 -0
  24. TinyMLC/templates/model.h.tpl +66 -0
  25. TinyMLC/transform/__init__.py +0 -0
  26. TinyMLC/transform/algebraic.py +286 -0
  27. TinyMLC/transform/base.py +58 -0
  28. TinyMLC/transform/constant_folding.py +260 -0
  29. TinyMLC/transform/cse.py +192 -0
  30. TinyMLC/transform/dce.py +182 -0
  31. TinyMLC/transform/fusion.py +723 -0
  32. TinyMLC/transform/memory.py +200 -0
  33. TinyMLC/transform/pass_manager.py +101 -0
  34. TinyMLC/transform/simplify.py +515 -0
  35. tinymlc-0.1.0.dist-info/METADATA +49 -0
  36. tinymlc-0.1.0.dist-info/RECORD +47 -0
  37. tinymlc-0.1.0.dist-info/WHEEL +4 -0
  38. tinymlc-0.1.0.dist-info/entry_points.txt +2 -0
  39. tinymlc-0.1.0.dist-info/licenses/LICENSE +201 -0
  40. utils/__init__.py +0 -0
  41. utils/arm-none-eabi-gcc.cmake +53 -0
  42. utils/dump.py +86 -0
  43. utils/generate_onnx_models.py +183 -0
  44. utils/generate_tflite_models.py +236 -0
  45. utils/pack_macos.sh +88 -0
  46. utils/path.py +31 -0
  47. utils/riscv-none-elf-gcc.cmake +50 -0
@@ -0,0 +1,236 @@
1
+ # -*- coding: utf-8 -*-
2
+ # TinyMLC - Tiny Machine Learning Compiler
3
+ #
4
+ # Copyright (c) 2026 Jia Liu & TinyMLC Contributors
5
+ # SPDX-License-Identifier: Apache-2.0
6
+ #
7
+ # This file is part of TinyMLC.
8
+ # Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at:
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
19
+
20
+ #!/usr/bin/env python3
21
+ # Generate multiple TFLite models covering all supported ops.
22
+
23
+ import tensorflow as tf
24
+
25
+ from pathlib import Path
26
+
27
+
28
+ def build_cnn_model():
29
+ """Conv2D, DepthwiseConv2D, MaxPool2D, AvgPool2D,
30
+ GlobalAvgPool2D, FC, Softmax, Relu"""
31
+ inputs = tf.keras.Input(shape=(28, 28, 1), name='input')
32
+ x = tf.keras.layers.Conv2D(8, 3, padding='same', name='conv2d')(inputs)
33
+ x = tf.keras.layers.ReLU(name='relu')(x)
34
+ x = tf.keras.layers.DepthwiseConv2D(
35
+ 3, padding='same', name='depthwise_conv2d')(x)
36
+ x = tf.keras.layers.ReLU(name='relu2')(x)
37
+ x = tf.keras.layers.MaxPooling2D(2, name='max_pool2d')(x)
38
+ x = tf.keras.layers.AveragePooling2D(2, name='avg_pool2d')(x)
39
+ x = tf.keras.layers.GlobalAveragePooling2D(name='global_avg_pool')(x)
40
+ x = tf.keras.layers.Dense(10, name='fc')(x)
41
+ outputs = tf.keras.layers.Softmax(name='softmax')(x)
42
+ model = tf.keras.Model(inputs=inputs, outputs=outputs, name='model_cnn')
43
+ return model
44
+
45
+
46
+ def build_activations_model():
47
+ """ReLU, LeakyReLU, ReLU6, PReLU, HardSigmoid, Sigmoid, Tanh, Clip"""
48
+ inputs = tf.keras.Input(shape=(16,), name='input')
49
+ x = tf.keras.layers.Dense(16, name='dummy')(inputs)
50
+ relu = tf.keras.layers.ReLU(name='relu')(x)
51
+ leaky = tf.keras.layers.LeakyReLU(alpha=0.1, name='leaky_relu')(relu)
52
+ relu6 = tf.keras.layers.ReLU(max_value=6.0, name='relu6')(leaky)
53
+ prelu = tf.keras.layers.PReLU(name='prelu')(relu6)
54
+ hs = tf.keras.layers.Activation('hard_sigmoid', name='hard_sigmoid')(prelu)
55
+ sig = tf.keras.layers.Activation('sigmoid', name='sigmoid')(hs)
56
+ tanh = tf.keras.layers.Activation('tanh', name='tanh')(sig)
57
+ clip = tf.keras.layers.Lambda(
58
+ lambda z: tf.clip_by_value(z, -2.0, 2.0), name='clip')(tanh)
59
+ outputs = clip
60
+ model = tf.keras.Model(
61
+ inputs=inputs, outputs=outputs, name='model_activations')
62
+ return model
63
+
64
+
65
+ def build_tensor_ops_model():
66
+ inputs = tf.keras.Input(shape=(8, 8, 4))
67
+ x = tf.keras.layers.Conv2D(4, 3, padding='same')(inputs)
68
+
69
+ # Split
70
+ splits = tf.keras.layers.Lambda(lambda z: tf.split(z, 2, axis=-1))(x)
71
+
72
+ # Pad channels only (H/W unchanged, only modify channel dimension)
73
+ pad = tf.keras.layers.Lambda(
74
+ lambda z: tf.pad(z, [[0,0], [0,0], [0,0], [1,1]]),
75
+ # [batch, H, W, channels]
76
+ name='pad'
77
+ )(splits[0])
78
+
79
+ # Concat back (shapes now match: H/W are both 8)
80
+ concat = tf.keras.layers.Concatenate(name='concat')([pad, splits[1]])
81
+
82
+ # Flatten
83
+ flat = tf.keras.layers.Flatten(name='flatten')(concat)
84
+
85
+ # Reshape
86
+ reshape = tf.keras.layers.Reshape((8, -1), name='reshape')(flat)
87
+
88
+ # Transpose
89
+ transpose = tf.keras.layers.Lambda(
90
+ lambda z: tf.transpose(z, perm=[0, 2, 1]),
91
+ name='transpose'
92
+ )(reshape)
93
+
94
+ # StridedSlice
95
+ slice_out = tf.keras.layers.Lambda(
96
+ lambda z: z[:, 0:4, 0:4],
97
+ name='strided_slice'
98
+ )(transpose)
99
+
100
+ return tf.keras.Model(
101
+ inputs=inputs, outputs=slice_out, name='model_tensor_ops')
102
+
103
+
104
+ def build_arithmetic_model():
105
+ """Add, Multiply, Subtract, ReduceSum, ReduceMean, ArgMax"""
106
+ inputs = tf.keras.Input(shape=(8, 8), name='input')
107
+ x = tf.keras.layers.Dense(8, name='dummy')(inputs)
108
+
109
+ # split into two branches
110
+ split = tf.keras.layers.Lambda(
111
+ lambda z: tf.split(z, 2, axis=-1), name='split')(x)
112
+ a = split[0]
113
+ b = split[1]
114
+
115
+ add = tf.keras.layers.Add(name='add')([a, b])
116
+ mul = tf.keras.layers.Multiply(name='multiply')([add, a])
117
+ sub = tf.keras.layers.Subtract(name='sub')([mul, add])
118
+
119
+ reduce_sum = tf.keras.layers.Lambda(
120
+ lambda z: tf.reduce_sum(z, axis=-1, keepdims=True),
121
+ name='reduce_sum'
122
+ )(sub)
123
+
124
+ reduce_mean = tf.keras.layers.Lambda(
125
+ lambda z: tf.reduce_mean(z, axis=-1, keepdims=True),
126
+ name='reduce_mean'
127
+ )(reduce_sum)
128
+
129
+ argmax = tf.keras.layers.Lambda(
130
+ lambda z: tf.argmax(z, axis=-1, output_type=tf.int32),
131
+ name='argmax'
132
+ )(reduce_mean)
133
+
134
+ outputs = tf.keras.layers.Lambda(
135
+ lambda z: tf.cast(z, tf.float32),
136
+ name='cast_out'
137
+ )(argmax)
138
+
139
+ model = tf.keras.Model(
140
+ inputs=inputs, outputs=outputs, name='model_arithmetic')
141
+ return model
142
+
143
+
144
+ def build_upsample_model():
145
+ """Upsample (via ResizeNearestNeighbor), ConvTranspose"""
146
+ inputs = tf.keras.Input(shape=(8, 8, 2), name='input')
147
+
148
+ # Conv2DTranspose
149
+ x = tf.keras.layers.Conv2DTranspose(
150
+ 4, 3, strides=2, padding='same', name='conv_transpose')(inputs)
151
+
152
+ # Upsample (ResizeNearestNeighbor)
153
+ upsample = tf.keras.layers.Lambda(
154
+ lambda z: tf.image.resize(z, (16, 16), method='nearest'),
155
+ name='upsample'
156
+ )(x)
157
+
158
+ outputs = upsample
159
+ model = tf.keras.Model(
160
+ inputs=inputs, outputs=outputs, name='model_upsample')
161
+ return model
162
+
163
+
164
+ def build_lstm_model():
165
+ """LSTM, SVDF (simulated with Dense for SVDF)"""
166
+ inputs = tf.keras.Input(shape=(10, 4), name='input')
167
+ lstm = tf.keras.layers.LSTM(8, return_sequences=False, name='lstm')(inputs)
168
+ # SVDF isn't standard in Keras, use Dense as proxy
169
+ svdf = tf.keras.layers.Dense(4, name='svdf')(lstm)
170
+ outputs = svdf
171
+ model = tf.keras.Model(inputs=inputs, outputs=outputs, name='model_lstm')
172
+ return model
173
+
174
+
175
+ def convert_to_tflite(model, name, output_dir=None):
176
+ """Convert Keras model to TFLite."""
177
+ if output_dir is None:
178
+ output_dir = Path(".")
179
+ output_dir = Path(output_dir)
180
+ output_dir.mkdir(parents=True, exist_ok=True)
181
+ output_path = output_dir / f'{name}.tflite'
182
+
183
+ model.build(model.input_shape)
184
+
185
+ # Convert with default settings
186
+ converter = tf.lite.TFLiteConverter.from_keras_model(model)
187
+ converter.optimizations = [tf.lite.Optimize.DEFAULT]
188
+ converter.target_spec.supported_types = [tf.int8]
189
+ converter.inference_input_type = tf.int8
190
+ converter.inference_output_type = tf.int8
191
+ try:
192
+ tflite = converter.convert()
193
+ with open(output_path, 'wb') as f:
194
+ f.write(tflite)
195
+ print(f' Saved: {output_path}')
196
+ return
197
+ except Exception as e:
198
+ print(f' Warning: int8 conversion failed: {e}')
199
+ print(f' Retrying with float and SELECT_TF_OPS...')
200
+
201
+ # Fallback: use float + SELECT_TF_OPS (for LSTM/SVDF)
202
+ converter = tf.lite.TFLiteConverter.from_keras_model(model)
203
+ converter.optimizations = [tf.lite.Optimize.DEFAULT]
204
+ converter.target_spec.supported_ops = [
205
+ tf.lite.OpsSet.TFLITE_BUILTINS,
206
+ tf.lite.OpsSet.SELECT_TF_OPS
207
+ ]
208
+ converter._experimental_lower_tensor_list_ops = False
209
+ try:
210
+ tflite = converter.convert()
211
+ with open(output_path, 'wb') as f:
212
+ f.write(tflite)
213
+ print(f' Saved: {output_path} (float + SELECT_TF_OPS)')
214
+ except Exception as e:
215
+ print(f' Error: {name} conversion failed: {e}')
216
+
217
+
218
+ def main():
219
+ models = [
220
+ ('model_cnn', build_cnn_model()),
221
+ ('model_activations', build_activations_model()),
222
+ ('model_tensor_ops', build_tensor_ops_model()),
223
+ ('model_arithmetic', build_arithmetic_model()),
224
+ ('model_upsample', build_upsample_model()),
225
+ ('model_lstm', build_lstm_model()),
226
+ ]
227
+
228
+ output_dir = Path("model_tests/tflite")
229
+ output_dir.mkdir(parents=True, exist_ok=True)
230
+
231
+ for name, model in models:
232
+ convert_to_tflite(model, name, output_dir=output_dir)
233
+
234
+
235
+ if __name__ == '__main__':
236
+ main()
utils/pack_macos.sh ADDED
@@ -0,0 +1,88 @@
1
+ #!/bin/bash
2
+ # pack_macos.sh - Package TinyMLC for macOS
3
+
4
+ set -e
5
+
6
+ SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
7
+ PROJECT_ROOT="$( cd "$SCRIPT_DIR/.." && pwd )"
8
+
9
+ APP_NAME="TinyMLC"
10
+ APP_DIR="${PROJECT_ROOT}/build/${APP_NAME}.app"
11
+ CONTENTS_DIR="${APP_DIR}/Contents"
12
+ RESOURCES_DIR="${CONTENTS_DIR}/Resources"
13
+ MACOS_DIR="${CONTENTS_DIR}/MacOS"
14
+ VERSION="0.1.0"
15
+
16
+ echo "🔨 Building TinyMLC GUI..."
17
+ cd ${PROJECT_ROOT}/TinyGUI
18
+ mkdir -p build
19
+ cd build
20
+ cmake ..
21
+ make
22
+
23
+ echo "📦 Creating app bundle structure..."
24
+ mkdir -p "${MACOS_DIR}"
25
+ mkdir -p "${RESOURCES_DIR}"
26
+
27
+ cd "${RESOURCES_DIR}"
28
+ uv venv -p 3.13 venv
29
+ source venv/bin/activate
30
+
31
+ echo "Current dir: $(pwd)"
32
+ echo "PROJECT_ROOT: ${PROJECT_ROOT}"
33
+ echo "RESOURCES_DIR: ${RESOURCES_DIR}"
34
+ echo "Looking for uv.lock at: ${PROJECT_ROOT}/uv.lock"
35
+ cp ${PROJECT_ROOT}/uv.lock .
36
+ uv sync --frozen
37
+ #uv pip install -e "${PROJECT_ROOT}" --python "${RESOURCES_DIR}/venv/bin/python"
38
+
39
+ echo "📝 Creating launch.sh..."
40
+ echo "RESOURCES_DIR: ${RESOURCES_DIR}"
41
+ cat > "${RESOURCES_DIR}/launch.sh" << 'EOF'
42
+ #!/bin/bash
43
+ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
44
+ cd "$DIR"
45
+ export PYTHONPATH="$DIR:$DIR/TinyMLC:$DIR/utils:$DIR/TinyMLC/ANG:$DIR/TinyMLC/converter"
46
+ ./venv/bin/python main.py "$@"
47
+ EOF
48
+ chmod +x "${RESOURCES_DIR}/launch.sh"
49
+
50
+
51
+ echo "📄 Copying executable..."
52
+ cp ${PROJECT_ROOT}/TinyGUI/build/TinyGUI "${MACOS_DIR}/${APP_NAME}"
53
+
54
+ echo "📄 Copying Python backend..."
55
+ cp ${PROJECT_ROOT}/main.py "${RESOURCES_DIR}/"
56
+ cp ${PROJECT_ROOT}/cli.py "${RESOURCES_DIR}/"
57
+ cp ${PROJECT_ROOT}/handlers.py "${RESOURCES_DIR}/"
58
+ cp -r ${PROJECT_ROOT}/TinyMLC "${RESOURCES_DIR}/"
59
+ cp -r ${PROJECT_ROOT}/TinyMLC/ANG "${RESOURCES_DIR}/"
60
+ cp -r ${PROJECT_ROOT}/TinyMLC/converter "${RESOURCES_DIR}/"
61
+ cp -r ${PROJECT_ROOT}/utils "${RESOURCES_DIR}/"
62
+
63
+ echo "📄 Copying Info.plist..."
64
+ cat > "${CONTENTS_DIR}/Info.plist" << EOF
65
+ <?xml version="1.0" encoding="UTF-8"?>
66
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
67
+ <plist version="1.0">
68
+ <dict>
69
+ <key>CFBundleExecutable</key>
70
+ <string>${APP_NAME}</string>
71
+ <key>CFBundleIdentifier</key>
72
+ <string>com.tinymlc.${APP_NAME}</string>
73
+ <key>CFBundleName</key>
74
+ <string>${APP_NAME}</string>
75
+ <key>CFBundleVersion</key>
76
+ <string>${VERSION}</string>
77
+ <key>CFBundleShortVersionString</key>
78
+ <string>${VERSION}</string>
79
+ <key>CFBundlePackageType</key>
80
+ <string>APPL</string>
81
+ </dict>
82
+ </plist>
83
+ EOF
84
+
85
+ echo "🔧 Running macdeployqt..."
86
+ macdeployqt "${APP_DIR}" -dmg
87
+
88
+ echo "✅ Done! App bundle created at: ${APP_DIR}"
utils/path.py ADDED
@@ -0,0 +1,31 @@
1
+ # -*- coding: utf-8 -*-
2
+ # TinyMLC - Tiny Machine Learning Compiler
3
+ #
4
+ # Copyright (c) 2026 Jia Liu & TinyMLC Contributors
5
+ # SPDX-License-Identifier: Apache-2.0
6
+ #
7
+ # This file is part of TinyMLC.
8
+ # Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at:
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
19
+
20
+ #!/usr/bin/env python3
21
+ """Path utilities for TinyMLC CLI"""
22
+
23
+ from pathlib import Path
24
+ from argparse import Namespace
25
+
26
+
27
+ def get_output_dir(args: Namespace) -> Path:
28
+ """Get and create output directory from args"""
29
+ out_dir = Path(getattr(args, "output_dir", "."))
30
+ out_dir.mkdir(parents=True, exist_ok=True)
31
+ return out_dir
@@ -0,0 +1,50 @@
1
+ # -*- coding: utf-8 -*-
2
+ # TinyMLC - Tiny Machine Learning Compiler
3
+ #
4
+ # Copyright (c) 2026 Jia Liu & TinyMLC Contributors
5
+ # SPDX-License-Identifier: Apache-2.0
6
+ #
7
+ # This file is part of TinyMLC.
8
+ # Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at:
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
19
+
20
+ # For building NMSIS-NN
21
+ # cmake ../Source \
22
+ # -DCMAKE_TOOLCHAIN_FILE=../riscv-none-elf-gcc.cmake \
23
+ # -DRISCV_ARCH=rv32imac \
24
+ # -DRISCV_ABI=ilp32 \
25
+ # -DRISCV_MODEL=medany
26
+ # make
27
+ # Then copy Include and libNMSISNN.a
28
+
29
+ set(CMAKE_SYSTEM_NAME Generic)
30
+ set(CMAKE_SYSTEM_PROCESSOR riscv)
31
+
32
+ set(CROSS_COMPILE "riscv-none-elf-")
33
+
34
+ set(CMAKE_C_COMPILER ${CROSS_COMPILE}gcc)
35
+ set(CMAKE_CXX_COMPILER ${CROSS_COMPILE}g++)
36
+ set(CMAKE_ASM_COMPILER ${CROSS_COMPILE}gcc)
37
+ set(CMAKE_AR ${CROSS_COMPILE}ar)
38
+ set(CMAKE_OBJCOPY ${CROSS_COMPILE}objcopy)
39
+ set(CMAKE_OBJDUMP ${CROSS_COMPILE}objdump)
40
+
41
+ set(RISCV_ARCH "rv32imac")
42
+ set(RISCV_ABI "ilp32")
43
+
44
+ add_compile_options(
45
+ -march=${RISCV_ARCH}
46
+ -mabi=${RISCV_ABI}
47
+ -mcmodel=medany
48
+ -O2
49
+ -Wall
50
+ )