Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/sparseml/transformers/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
"""

import argparse
import collections
import inspect
import logging
import math
import os
Expand Down Expand Up @@ -180,13 +182,32 @@ def export_transformer_to_onnx(
inputs = tokenizer(
"", return_tensors="pt", padding=PaddingStrategy.MAX_LENGTH.value
).data # Dict[Tensor]

# Rearrange inputs' keys to match those defined by model foward func, which
# seem to define how the order of inputs is determined in the exported model
forward_args_spec = inspect.getfullargspec(model.__class__.forward)
dropped = [f for f in inputs.keys() if f not in forward_args_spec.args]
inputs = collections.OrderedDict(
[
(f, inputs[f][0].reshape(1, -1))
for f in forward_args_spec.args
if f in inputs
]
)
if dropped:
_LOGGER.warning(
"The following inputs were not present in the model forward function "
f"and therefore dropped from ONNX export: {dropped}"
)

inputs_shapes = {
key: (
f"{val.dtype if hasattr(val, 'dtype') else 'unknown'}: "
f"{list(val.shape) if hasattr(val, 'shape') else 'unknown'}"
)
for key, val in inputs.items()
}

_LOGGER.info(f"Created sample inputs for the ONNX export process: {inputs_shapes}")

# run export
Expand Down