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
@justinchuby
justinchuby / Exported program bundle.txt
Created May 24, 2025 01:58
Exported program bundle
https://github.com/iree-org/iree-turbine/blob/main/iree/turbine/aot/fx_programs.py
Also ai-edge torch exporter
@justinchuby
justinchuby / stable.py
Last active May 23, 2025 04:35
Stable HLO
from ai_edge_torch.odml_torch.export import exported_program_to_mlir
import torch
class PowModel(torch.nn.Module):
def forward(self, x: torch.Tensor) -> torch.Tensor:
return x ** 0.5
model = PowModel()
print(model(torch.tensor(2)))
@justinchuby
justinchuby / export_hf.py
Created April 22, 2025 23:39
Export HF models with torch.onnx
import torch
from onnx_diagnostic import torch_export_patches
from onnxscript.ir.passes.common import clear_metadata_and_docstring
from transformers import AttentionInterface, AutoModelForCausalLM, AutoTokenizer
from transformers.cache_utils import DynamicCache
# Get position_ids from attention_mask
def get_position_ids(attention_mask: torch.Tensor, use_past_kv: bool):
# Owner(s): ["module: onnx"]
"""Unit LLM tests for the onnx dynamo exporter."""
from __future__ import annotations
from typing import Any
import logging
import transformers
@justinchuby
justinchuby / torch_geometric_onnx_comp.py
Last active March 7, 2025 00:13
Code for figuring out where an onnx model is inaccurate and visualize with model explorer
import logging
import torch
from torch_geometric.nn import GAT
logger = logging.getLogger(__name__)
logging.getLogger('torch.onnx').setLevel(logging.INFO)
logger.info("Prepare model")
num_features = 23
num_classes = 12
@justinchuby
justinchuby / ops.py
Last active February 27, 2025 17:34
PyTorch ONNX exporter supported ops
"""Display all PyTorch ONNX exporter supported ops.
NOTE: This is using internal methods. Do not use it in production code.
NOTE: Ops implemented via decomp may not be supported because they may still be decomposed
into ops that are without native implementation. They include some backward ops,
svd, sq, and some others.
"""
from torch.onnx._internal.exporter import _decomp, _registration
@justinchuby
justinchuby / constants_to_initializers.py
Last active April 13, 2025 15:12
Convert constants to initializers with ONNX IR
# **************************************************************************************
# NOTE: Users can now use https://github.com/microsoft/onnxscript/blob/main/onnxscript/ir/passes/common/constant_manipulation.py
# aka. onnxscript.ir.passes.common.constant_manipulation.LiftConstantsToInitializersPass
# **************************************************************************************
from onnxscript import ir
def convert_constants_to_initizliers(model: ir.Model, size_limit: int = 1024):
"""Convert constant nodes to initializers."""
for node in model.graph:
@justinchuby
justinchuby / fold_transpose.py
Last active January 23, 2025 19:05
Fold transpose nodes with ONNX IR
from onnxscript import ir
def fold_transpose_initializers(model: ir.Model):
for name, initializer in model.graph.initializers.items():
user_nodes = initializer.consumers()
if len(user_nodes) == 1 and user_nodes[0].op_type == "Transpose":
transpose_node = user_nodes[0]
perm = transpose_node.attributes.get("perm")
if perm is None:
@justinchuby
justinchuby / rename_dynamic_axes.py
Created January 16, 2025 23:28
Rename dynamic axes in ONNX model with IR
from onnxscript import ir
def _all_values(model: ir.Model):
"""Yield all values in a model."""
yield from model.graph.inputs
yield from model.graph.initializers.values()
for node in ir.traversal.RecursiveGraphIterator(model.graph):
yield from node.outputs
@justinchuby
justinchuby / PKG-INFO
Created January 14, 2025 01:34
Dynamic project url
Metadata-Version: 2.2
Name: onnxscript
Version: 0.1.0.dev20250113
Summary: Naturally author ONNX functions and models using a subset of Python
Author-email: Microsoft Corporation <[email protected]>
License: MIT License
Copyright (c) Microsoft Corporation
Permission is hereby granted, free of charge, to any person obtaining a copy