Skip to content

Instantly share code, notes, and snippets.

View justinchuby's full-sized avatar
🌊
Better ML

Justin Chu justinchuby

🌊
Better ML
View GitHub Profile
import onnx_ir as ir
import onnx_ir.passes.common
import onnxscript
m = ir.load("perch_v2_opt3.onnx")
for node in m.graph:
if node.op_type == "MatMul":
print(node)
if node.inputs[0].producer().op_type == "Reshape":
@justinchuby
justinchuby / ort_error.txt
Created November 5, 2025 02:27
ORT error
Loading ONNX model from: perch_v2_opt2.onnx
2025-11-04 18:25:33.374 python[72171:12211235] 2025-11-04 18:25:33.374513 [W:onnxruntime:, graph.cc:4885 CleanUnusedInitializersAndNodeArgs] Removing initializer 'arith.constant470'. It is not used by any node and should be removed from the model.
2025-11-04 18:25:33.414 python[72171:12211235] 2025-11-04 18:25:33.414751 [E:onnxruntime:, inference_session.cc:2544 operator()] Exception during initialization: /Users/runner/work/1/s/onnxruntime/core/framework/execution_frame.h:112 const OrtValue &onnxruntime::IExecutionFrame::GetMLValue(int) const ort_value_index >= 0 && static_cast<size_t>(ort_value_index) < all_values_size_ was false.
Traceback (most recent call last):
File "/Users/justinc/Documents/GitHub/tensorflow-onnx/examples/compare_onnx_tflite.py", line 599, in <module>
exit(main())
^^^^^^
File "/Users/justinc/Documents/GitHub/tensorflow-onnx/examples/compare_onnx_tflite.py", line 541, in main
onnx_session = load_onnx_model(args.onnx)
@justinchuby
justinchuby / compare_onnx_tflite.py
Created November 2, 2025 17:25
Script to compare tflite and ONNX models
#!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
"""
Script to compare the results of an ONNX model with a TFLite model given the same input.
Optionally also compare with Tract runtime for ONNX.
Created by Copilot.
Usage:
python compare_onnx_tflite.py --onnx model.onnx --tflite model.tflite
@justinchuby
justinchuby / qwen3_onnx.py
Last active October 28, 2025 23:42
Qwen3EmbeddingONNXExporter
# transformers==4.52.4
# pytorch nightly
from __future__ import annotations
import ast
import logging
import typing
from pathlib import Path
import onnx_ir as ir
{%- if tools %}
{{- '<|im_start|>system\n' }}
{%- if messages[0].role == 'system' %}
{{- messages[0].content + '\n\n' }}
{%- endif %}
{{- "# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}
{%- for tool in tools %}
{{- "\n" }}
{{- tool | tojson }}
{%- endfor %}
2025-10-17 17:18:33.408 -07:00 [INF] Command:ModelInit Status:Success Direct:True Time:0ms
2025-10-17 17:18:33.861 -07:00 [INF] UserAgent:foundry-local-CLI/0.7.120+3b92ed4014 Command:ListDownloadedModels Status:Success Direct:True Time:5ms
2025-10-17 17:18:33.861 -07:00 [INF] Command:ModelDownload Status:Skipped Direct:False Time:1698ms
2025-10-17 17:18:33.861 -07:00 [INF] Command:ServiceStart Status:Skipped Direct:False Time:0ms
2025-10-17 17:18:34.234 -07:00 [INF] Command:ServiceList Status:Success Direct:False Time:372ms
2025-10-17 17:18:34.234 -07:00 [INF] Loading model: http://127.0.0.1:56051/openai/load/gemma-3-1b-it_ort?ttl=600
2025-10-17 17:18:34.234 -07:00 [INF] UserAgent:foundry-local-CLI/0.7.120+3b92ed4014 Command:ListLoadedModels Status:Success Direct:True Time:0ms
2025-10-17 17:18:34.685 -07:00 [INF] Loading model:gemma-3-1b-it_ort
2025-10-17 17:18:40.704 -07:00 [INF] Finish loading model:gemma-3-1b-it_ort elapsed time:00:00:06.0190199
2025-10-17 17:18:40.704 -07:00 [INF] UserAgent:foundry-lo
import torch
import onnx_ir as ir
class ControlFlowModel(torch.nn.Module):
def forward(self, x):
def times_2(x):
return x * 2
def neg(x):
return -x
@justinchuby
justinchuby / export_hf.py
Last active October 14, 2025 04:33
Export HF model to ONNX
"""Export to ONNX.
transformers_version == "4.52.0"
"""
import onnx_diagnostic.tasks.text_generation
import torch
from transformers import AutoConfig, AutoModel
import onnxscript
import onnx_ir as ir
import onnx
def create_model():
"""Create a model that has a unsorted node with subgraph that uses a value defined later."""
a = ir.Value(name="a", type=ir.TensorType(ir.DataType.FLOAT), shape=ir.Shape((2, 3)))
b = ir.Value(name="b", type=ir.TensorType(ir.DataType.FLOAT), shape=ir.Shape((3, 4)))
b_out = ir.Value(name="b_out", type=ir.TensorType(ir.DataType.FLOAT), shape=ir.Shape((3, 4)))
c = ir.Value(name="c", type=ir.TensorType(ir.DataType.FLOAT), shape=ir.Shape((4, 5)))
@justinchuby
justinchuby / pt_export.md
Created August 13, 2025 18:44
PyTorch export non-strict vs strict modes

torch.export non strict mode uses "make_fx [which] uses __torch_dispatch__ to trace under the hood. this is where it creates the fx nodes. AOTAutograd also calls into make_fx, but before that it also does some things related to functionalization. Since export is now "Training IR", it no longer does functionalization, so we just directly call make_fx."