Optimum ONNX Runtime API improvement#515
Conversation
|
The documentation is not available anymore as the PR was closed or merged. |
4ba8d37 to
ff0042b
Compare
2d8b31d to
f503ea6
Compare
| feature_extractor.save_pretrained(args.output.parent) | ||
| except Exception: | ||
| pass | ||
| maybe_save_tokenizer_or_processor_or_feature_extractor(args.model, args.output.parent) |
There was a problem hiding this comment.
I remember discussing to move this inside export, is that still the case?
There was a problem hiding this comment.
I actually do not know if we should put this inside the export itself, good question.
| task, | ||
| model_id, | ||
| subfolder=subfolder, | ||
| revision=revision, | ||
| cache_dir=cache_dir, | ||
| config=config, | ||
| use_auth_token=use_auth_token, | ||
| local_files_only=local_files_only, | ||
| force_download=force_download, | ||
| ) | ||
|
|
||
| model_type = model.config.model_type.replace("_", "-") | ||
| model_name = getattr(model, "name", None) | ||
|
|
||
| onnx_config_constructor = TasksManager.get_exporter_config_constructor( | ||
| model_type, "onnx", task=task, model_name=model_name | ||
| ) | ||
| onnx_config = onnx_config_constructor(model.config, use_present_in_outputs=True) |
There was a problem hiding this comment.
I see this being repeated in multiple files. Would it make sense to have a helper function for these lines?
There was a problem hiding this comment.
I tried implementing it, but end up with circular imports!
| os.makedirs(save_directory, exist_ok=True) | ||
|
|
||
| self.config.save_pretrained(save_directory) | ||
| for preprocessor in self._preprocessors: |
There was a problem hiding this comment.
PreTrainedModel does not save preprocessors with save_pretrained, why would we introduce this asymmetry in Optimum? Personnally I don't understand the motivation for this self._preprocessors. What's the issue with fully dissociating preprocessing and modeling, like it's done in transformers?
I also think it would be better to break down these huge PRs in smaller ones
There was a problem hiding this comment.
If it is not there, we do not do anything, otherwise we save it. The reason for this is because let's say I export a model with the ORTModel classes, I want to be able to use the save directory as a model repo end-to-end. Same thing for graph optimization / quantization. Otherwise we would need to provide a different name for the ORTModel.from_pretrained and the AutoTokenizer.from_pretrained.
| os.path.join(model_id, subfolder, decoder_with_past_file_name) if use_cache else None | ||
| model_path = Path(model_id) | ||
|
|
||
| def validate_filename(filename): |
There was a problem hiding this comment.
Would this be better out of _from_pretrained. Otherwise this is redefined every time _from_pretrained is called, am I correct?
There was a problem hiding this comment.
Yes, which is not a big deal for functions with a one-time usage.
There was a problem hiding this comment.
why have it as a function in the first time then?
There was a problem hiding this comment.
We use it multiple time inside this function, but never outside. We could make that a method, but that is not needed specifically. While it is true that there is redundancy between this and modeling_seq2seq.py, I wanted to keep the function simple (making it a method woulrd require more arguments and so on).
| succeeded = False | ||
| return succeeded | ||
|
|
||
| def infer_filename(pattern: str, argument_name: str, fail_if_not_found: bool = True) -> str: |
There was a problem hiding this comment.
Same as above (Can we add a short comment as to what this function does + wouldn't it be better out)
There was a problem hiding this comment.
I do not think this is needed, this is basically a temporary function.
There was a problem hiding this comment.
I believe the name is enough, I renamed the first one since the name was not explicit.
| from ..exporters import TasksManager | ||
| from ..exporters.onnx import export | ||
| from ..modeling_base import FROM_PRETRAINED_START_DOCSTRING, OptimizedModel | ||
| from ..utils.save_utils import maybe_load_preprocessors, maybe_save_tokenizer_or_processor_or_feature_extractor |
There was a problem hiding this comment.
At first glance I don't understand what the maybe means in the functions name
There was a problem hiding this comment.
Because it might load / save or not. If nothing is found, it won't do anything.
| ): | ||
| io_binding = self.session.io_binding() | ||
|
|
||
| if "TensorrtExecutionProvider" in self.providers and self.use_io_binding: |
There was a problem hiding this comment.
Is this removed? I could not find it elsewhere. If removed, why?
There was a problem hiding this comment.
It is in ORTModel, now ORTModelForConditionalGeneration uses the base class initialiazer, so we do not need that anymore here.
|
Why was it merged? I would have liked to rereview and I don't think all my points were addressed. |
|
Which point do you want to discuss? We can discuss them, and I will make the PR for those changes (if needed). |
|
#515 (comment) => I still don't understand why we introduce #515 (comment) => could not find a description #515 (comment) => same, I disagree it is not needed, it would help readability #515 (comment) => could not find a documentation, but could have missed it |
Example: You load With model_name = "bert_quantized"
tok = AutoTokenizer.from_pretrained(model_name)
quantized_model = ORTModel.from_pretrained(model_name)instead of: tok = AutoTokenizer.from_pretrained("bert-base-uncased")
quantized_model = ORTModel.from_pretrained("bert_quantized")This makes things a lot easier in terms of usage, especially if we think about production.
|
|
@michaelbenayoun What is the difference between the two code samples? |
What does this PR do?
ORTModel,ORTModelDecoderandORTModelForConditionalGenerationcan load any ONNX model files regardless of their names, allowing to load optimized and quantized models without having to specify thefile_nameargument.ORTModel._from_transformersnow downloads and loads the model in a temporary directory instead of the cache, which was not a right place to store it.ORTQuantizersaves both the model configuration and the tokenizer / processor / feature extractor, making the exported directory usable end-to-end.ORTOptimizerwas already saving the model configuration but not the tokenize / processor / feature extractor, it does now.OnnxConfigWithPastnow has two class atributes (that will be used when no value is specified in the__init__to avoid having to overwrite the__init__just for that) :USE_PAST_FOR_INPUTS: if True, past key values will be in the inputs of the modelUSE_PRESENT_FOR_OUTPUTS: if True, present key values will be in the outputs of the modelThose features combined allow the following:
This was not possible before.
Fixes: #493