Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Allow ORTQuantizer over models with subfolder ONNX files #2094

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 5 additions & 4 deletions optimum/onnxruntime/quantization.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def __init__(self, onnx_model_path: Path, config: Optional["PretrainedConfig"] =
if self.config is None:
try:
self.config = AutoConfig.from_pretrained(self.onnx_model_path.parent)
except OSError:
except (OSError, ValueError):
LOGGER.warning(
f"Could not load the config for {self.onnx_model_path} automatically, this might make "
"the quantized model harder to use because it will not be able to be loaded by an ORTModel without "
Expand Down Expand Up @@ -134,6 +134,7 @@ def from_pretrained(
model_or_path = Path(model_or_path)

path = None
config = None
if isinstance(model_or_path, ORTModelForConditionalGeneration):
raise NotImplementedError(ort_quantizer_error_message)
elif isinstance(model_or_path, Path) and file_name is None:
Expand All @@ -147,13 +148,13 @@ def from_pretrained(
file_name = onnx_files[0].name

if isinstance(model_or_path, ORTModel):
if path is None:
path = Path(model_or_path.model._model_path)
path = Path(model_or_path.model._model_path)
config = model_or_path.config
elif os.path.isdir(model_or_path):
path = Path(model_or_path) / file_name
else:
raise ValueError(f"Unable to load model from {model_or_path}.")
return cls(path)
return cls(path, config=config)

def fit(
self,
Expand Down
8 changes: 8 additions & 0 deletions tests/onnxruntime/test_quantization.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
AutoQuantizationConfig,
ORTConfig,
ORTModelForCausalLM,
ORTModelForFeatureExtraction,
ORTModelForSeq2SeqLM,
ORTModelForSequenceClassification,
ORTQuantizer,
Expand All @@ -52,6 +53,13 @@ class ORTQuantizerTest(unittest.TestCase):
"optimum/distilbert-base-uncased-finetuned-sst-2-english"
)
},
"ort_model_with_onnx_model_in_subfolder": {
"model_or_path": ORTModelForFeatureExtraction.from_pretrained(
"sentence-transformers/all-MiniLM-L6-v2",
subfolder="onnx",
file_name="model.onnx",
)
},
}

@parameterized.expand(LOAD_CONFIGURATION.items())
Expand Down
Loading