Skip to main content

Copyright (C) 2025 The HYPERONNX Authors.

Project description

🚀 HYPER-ONNX

中文|EN

Hyper-ONNX can export pytorch models (nn.Module) in a hierarchical manner. It can keep the module hier information and make a nested onnx graph. ✨

📦 Install

Simply install from pypi:

pip install hyperonnx

Or you may install from source:

git clone https://github.com/LoSealL/hyperonnx.git
pip install -e hyperonnx[test]

🧪 Usage Example

1) Export nn.Module with specified hier info

import torch
import torchvision as tv
from torchvision.models.resnet import BasicBlock, Bottleneck, ResNet

from hyperonnx import export_hyper_onnx

model = tv.models.resnet18()
export_hyper_onnx(
    resnet,
    (torch.randn(1, 3, 224, 224),),
    "hyper-resnet18.onnx",
    input_names=["img"],
    output_names=["features"],
    hiera=[ResNet, BasicBlock, Bottleneck],
    do_optimization=False,
    dynamo=False,
)

r18-sample

2) Export any call to a model by auto tracing

from hyperonnx import auto_trace_method
from hyperonnx.patch import patch_transformers
from transformers import (
    GenerationConfig,
    Qwen2_5OmniThinkerForConditionalGeneration,
)
from transformers.models.qwen2_5_omni.modeling_qwen2_5_omni import (
    Qwen2_5_VisionPatchEmbed,
    Qwen2_5_VisionRotaryEmbedding,
    Qwen2_5OmniAudioEncoderLayer,
    Qwen2_5OmniDecoderLayer,
    Qwen2_5OmniPatchMerger,
    Qwen2_5OmniVisionBlock,
)

thinker = Qwen2_5OmniThinkerForConditionalGeneration.from_pretrained(
    "Qwen/Qwen2.5-Omni-3B",
    dtype="float16",
    device_map="cuda",
)
with (
    patch_transformers(),
    auto_trace_method(thinker.model.forward) as text_tracer,
    auto_trace_method(thinker.visual.forward) as visual_tracer,
    auto_trace_method(thinker.audio_tower.forward) as audio_tracer,
):
    try:
        outputs = thinker.generate(
            **inputs,  # your any input data
            max_new_tokens=2048,
            generation_config=GenerationConfig(use_cache=False),
        )
    except StopIteration:
        pass
    text_tracer.export(
        "qwen-omni-2.5-3b-text.onnx",
        input_names=["input_ids"],
        output_names=["hidden_states"],
        hiera=[
            Qwen2_5OmniDecoderLayer,
        ],
        external_data=True,
        external_directory="qwen25_omni/text",
        do_optimization=True,
    )
    visual_tracer.export(
        "qwen-omni-2.5-3b-vision.onnx",
        input_names=["hidden_states"],
        output_names=["last_hidden_state"],
        hiera=[
            Qwen2_5_VisionPatchEmbed,
            Qwen2_5_VisionRotaryEmbedding,
            Qwen2_5OmniVisionBlock,
            Qwen2_5OmniPatchMerger,
        ],
        external_data=True,
        external_directory="qwen25_omni/vision",
        do_optimization=True,
    )
    audio_tracer.export(
        "qwen-omni-2.5-3b-audio.onnx",
        input_names=["hidden_states"],
        output_names=["last_hidden_state"],
        hiera=[
            Qwen2_5OmniAudioEncoderLayer,
        ],
        external_data=True,
        external_directory="qwen25_omni/audio",
        do_optimization=True,
    )

qwen2

If you run into issues or want to contribute, feel free to open an Issue or PR. 💡

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hyperonnx-1.0.1.tar.gz (4.6 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hyperonnx-1.0.1-py3-none-any.whl (34.8 kB view details)

Uploaded Python 3

File details

Details for the file hyperonnx-1.0.1.tar.gz.

File metadata

  • Download URL: hyperonnx-1.0.1.tar.gz
  • Upload date:
  • Size: 4.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for hyperonnx-1.0.1.tar.gz
Algorithm Hash digest
SHA256 1ca1cc3b443b6b404f8ad5783b2c65323b3508dac97cf0129fde3e131985d381
MD5 05d69f5d4b9c077603e998bf9f582ec2
BLAKE2b-256 ba72b10661fc63309524a1b4b79dd3d0d600ebf43c528dcbcfc0a7d8c04742f8

See more details on using hashes here.

File details

Details for the file hyperonnx-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: hyperonnx-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 34.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for hyperonnx-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e942931ee091d6dbba6d9f37c9057256f1fc17e3975ce90dc183bf66b88d87e4
MD5 523855199e55c233978f1a1fd2206d0b
BLAKE2b-256 edba3ff9401c4197aadae7a39a881c0b6f06bce84af5ca4107e101ec6475563c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page