Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
7e9ed74
wip
fxmarty Mar 28, 2023
d230923
first version
fxmarty Mar 28, 2023
6ee453c
add constants
fxmarty Mar 28, 2023
e5e35d7
wip
fxmarty Mar 28, 2023
054c529
wip
fxmarty Mar 28, 2023
1331f5f
fix shapes
fxmarty Mar 28, 2023
e2dd6b2
fix tests
fxmarty Mar 28, 2023
d04e215
fix more tests
fxmarty Mar 28, 2023
7424870
fix broken longt5
fxmarty Mar 28, 2023
0d8f621
fix again longt5
fxmarty Mar 28, 2023
a7218f6
Merge branch 'master' into support-merged-seq2seq-in-ortmodel
fxmarty Mar 29, 2023
aacf604
fix bugs and add tests
fxmarty Mar 29, 2023
827a92c
fix a few tests
fxmarty Mar 29, 2023
a02a25b
fix more tests
fxmarty Mar 29, 2023
c944ab7
slight bug
fxmarty Mar 29, 2023
911b505
slight bug
fxmarty Mar 29, 2023
c185aaa
fix iobinding
fxmarty Mar 29, 2023
a070d04
fix on suggestion
fxmarty Mar 29, 2023
5f8478d
Update optimum/onnxruntime/base.py
fxmarty Mar 29, 2023
6460cf7
typo
fxmarty Mar 29, 2023
adb88f9
Merge branch 'support-merged-seq2seq-in-ortmodel' of https://github.c…
fxmarty Mar 29, 2023
952d6df
trigger workflow
fxmarty Mar 29, 2023
b1b0b42
fix tests again
fxmarty Mar 29, 2023
43374a2
simplify save_pretrained
fxmarty Mar 29, 2023
87a7e95
fix type hint on suggestion
fxmarty Mar 29, 2023
56e93d3
simplification
fxmarty Mar 29, 2023
a5bc8d7
fix regex
fxmarty Mar 29, 2023
46bd828
fix from pretrained
fxmarty Mar 30, 2023
6c63737
typo
fxmarty Mar 30, 2023
16a1d76
Merge branch 'master' into support-merged-seq2seq-in-ortmodel
fxmarty Mar 30, 2023
7e98e15
style on merge
fxmarty Mar 30, 2023
4b3579b
fix
fxmarty Mar 30, 2023
1a2e97b
fix
fxmarty Mar 30, 2023
4729ca3
Update optimum/onnxruntime/base.py
fxmarty Mar 31, 2023
440d5a1
Update optimum/onnxruntime/base.py
fxmarty Mar 31, 2023
abe327d
fix on suggestion
fxmarty Mar 31, 2023
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
7 changes: 5 additions & 2 deletions optimum/exporters/onnx/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,8 +753,11 @@ def add_past_key_values(self, inputs_or_outputs: Dict[str, Dict[int, str]], dire
or (self._behavior is ConfigBehavior.DECODER and self.use_past is False)
or direction == "inputs"
):
inputs_or_outputs[f"{name}.{i}.encoder.key"] = {0: "batch_size", 2: "encoder_sequence_length"}
inputs_or_outputs[f"{name}.{i}.encoder.value"] = {0: "batch_size", 2: "encoder_sequence_length"}
# TODO: we only need to call it encoder_sequence_length_out in the merge case - but at torch.onnx.export()
# time we have currently no case to check whether we will merge at a later step or not (self.is_merged is
# not yet set at this time)
inputs_or_outputs[f"{name}.{i}.encoder.key"] = {0: "batch_size", 2: "encoder_sequence_length_out"}
inputs_or_outputs[f"{name}.{i}.encoder.value"] = {0: "batch_size", 2: "encoder_sequence_length_out"}

def flatten_past_key_values(self, flattened_output, name, idx, t):
flattened_output[f"{name}.{idx}.decoder.key"] = t[0]
Expand Down
219 changes: 165 additions & 54 deletions optimum/onnxruntime/base.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion optimum/onnxruntime/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
# limitations under the License.

ENCODER_ONNX_FILE_PATTERN = r"(.*)?encoder(.*)?\.onnx"
DECODER_ONNX_FILE_PATTERN = r"(.*)?decoder((?!with_past).)*?\.onnx"
DECODER_ONNX_FILE_PATTERN = r"(.*)?decoder((?!(with_past|merged)).)*?\.onnx"
DECODER_WITH_PAST_ONNX_FILE_PATTERN = r"(.*)?decoder(.*)?with_past(.*)?\.onnx"
DECODER_MERGED_ONNX_FILE_PATTERN = r"(.*)?decoder(.*)?merged(.*)?\.onnx"
122 changes: 62 additions & 60 deletions optimum/onnxruntime/modeling_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ def _save_pretrained(self, save_directory: Union[str, Path]):
for src_path, dst_path in zip(src_paths, dst_paths):
shutil.copyfile(src_path, dst_path)

self.generation_config.save_pretrained(save_directory)

@classmethod
def _from_pretrained(
cls,
Expand Down Expand Up @@ -330,6 +332,7 @@ def _from_pretrained(
revision=revision,
)
use_merged = True
decoder_path = decoder_merged_path
except FileNotFoundError as e:
if use_merged is True:
raise FileNotFoundError(
Expand All @@ -339,74 +342,74 @@ def _from_pretrained(
)
use_merged = False

if not validate_file_exists(model_id, decoder_file_name, subfolder=subfolder, revision=revision):
decoder_without_past_path = ORTModelDecoder.infer_onnx_filename(
model_id,
[DECODER_ONNX_FILE_PATTERN],
"decoder_file_name",
subfolder=subfolder,
use_auth_token=use_auth_token,
revision=revision,
)
else:
decoder_without_past_path = model_path / subfolder / decoder_file_name

if use_merged is True:
decoder_path = decoder_merged_path
else:
decoder_path = decoder_without_past_path

decoder_regular_onnx_filenames = ORTModelDecoder._generate_regular_names_for_filename(ONNX_DECODER_NAME)
if decoder_path.name not in decoder_regular_onnx_filenames:
logger.warning(
f"The ONNX file {decoder_path.name} is not a regular name used in optimum.onnxruntime that are {decoder_regular_onnx_filenames}, the "
f"{cls.__name__} might not behave as expected."
)

# If the decoder without / with past has been merged, we do not need to look for any additional file
decoder_without_past_path = None
decoder_with_past_path = None
if not validate_file_exists(model_id, decoder_with_past_file_name, subfolder=subfolder, revision=revision):
try:
decoder_with_past_path = ORTModelDecoder.infer_onnx_filename(
if use_merged is False:
if not validate_file_exists(model_id, decoder_file_name, subfolder=subfolder, revision=revision):
decoder_without_past_path = ORTModelDecoder.infer_onnx_filename(
model_id,
[DECODER_WITH_PAST_ONNX_FILE_PATTERN],
"decoder_with_past_file_name",
[DECODER_ONNX_FILE_PATTERN],
"decoder_file_name",
subfolder=subfolder,
use_auth_token=use_auth_token,
revision=revision,
)
except FileNotFoundError as e:
if use_cache is True and use_merged is False:
raise FileNotFoundError(
"The parameter `use_cache=True` was passed to ORTModelForCausalLM.from_pretrained()"
" but no ONNX file using past key values could be found in"
f" {str(Path(model_id, subfolder))}, with the error: {e}"
)
else:
decoder_with_past_path = model_path / subfolder / decoder_with_past_file_name
else:
decoder_without_past_path = model_path / subfolder / decoder_file_name

if use_cache is True and use_merged is False:
decoder_with_past_regular_onnx_filenames = ORTModelDecoder._generate_regular_names_for_filename(
ONNX_DECODER_WITH_PAST_NAME
)
decoder_path = decoder_without_past_path

if decoder_with_past_path.name not in decoder_with_past_regular_onnx_filenames:
decoder_regular_onnx_filenames = ORTModelDecoder._generate_regular_names_for_filename(ONNX_DECODER_NAME)
if decoder_path.name not in decoder_regular_onnx_filenames:
logger.warning(
f"The ONNX file {decoder_with_past_path.name} is not a regular name used in optimum.onnxruntime that are {decoder_with_past_regular_onnx_filenames}, "
f"the {cls.__name__} might not behave as expected."
f"The ONNX file {decoder_path.name} is not a regular name used in optimum.onnxruntime that are {decoder_regular_onnx_filenames}, the "
f"{cls.__name__} might not behave as expected."
)

# If the decoder without / with past has been merged, we do not need to look for any additional file
if use_cache is True:
if not validate_file_exists(
model_id, decoder_with_past_file_name, subfolder=subfolder, revision=revision
):
try:
decoder_with_past_path = ORTModelDecoder.infer_onnx_filename(
model_id,
[DECODER_WITH_PAST_ONNX_FILE_PATTERN],
"decoder_with_past_file_name",
subfolder=subfolder,
use_auth_token=use_auth_token,
revision=revision,
)
except FileNotFoundError as e:
raise FileNotFoundError(
"The parameter `use_cache=True` was passed to ORTModelForCausalLM.from_pretrained()"
" but no ONNX file using past key values could be found in"
f" {str(Path(model_id, subfolder))}, with the error: {e}"
)
else:
decoder_with_past_path = model_path / subfolder / decoder_with_past_file_name

decoder_with_past_regular_onnx_filenames = ORTModelDecoder._generate_regular_names_for_filename(
ONNX_DECODER_WITH_PAST_NAME
)

if decoder_with_past_path.name not in decoder_with_past_regular_onnx_filenames:
logger.warning(
f"The ONNX file {decoder_with_past_path.name} is not a regular name used in optimum.onnxruntime that are {decoder_with_past_regular_onnx_filenames}, "
f"the {cls.__name__} might not behave as expected."
)

preprocessors = None
if model_path.is_dir():
new_model_save_dir = model_path
preprocessors = maybe_load_preprocessors(model_id)
else:
attribute_name_to_filename = {
"last_decoder_model_name": decoder_path.name,
"last_decoder_model_name": decoder_path.name if use_merged is False else None,
"last_decoder_with_past_model_name": decoder_with_past_path.name
if decoder_with_past_path is not None
if (use_cache is True and use_merged is False)
else None,
"last_decoder_merged_name": decoder_merged_path.name if decoder_merged_path is not None else None,
"last_decoder_merged_name": decoder_merged_path.name if use_merged is True else None,
}
paths = {}
for attr_name, filename in attribute_name_to_filename.items():
Expand Down Expand Up @@ -443,18 +446,15 @@ def _from_pretrained(
new_model_save_dir = Path(model_cache_path).parent
preprocessors = maybe_load_preprocessors(model_id, subfolder=subfolder)

last_decoder_with_past_name = paths.get("last_decoder_with_past_model_name", None)
if last_decoder_with_past_name is not None:
decoder_with_past_path = new_model_save_dir / last_decoder_with_past_name

if use_merged is True:
decoder_path = new_model_save_dir / paths["last_decoder_merged_name"]
decoder_merged_path = new_model_save_dir / paths["last_decoder_merged_name"]
else:
decoder_path = new_model_save_dir / paths["last_decoder_model_name"]
decoder_without_past_path = new_model_save_dir / paths["last_decoder_model_name"]

decoder_without_past_path = new_model_save_dir / paths["last_decoder_model_name"]
if decoder_merged_path is not None:
decoder_merged_path = new_model_save_dir / paths["last_decoder_merged_name"]
if use_cache is True:
decoder_with_past_path = new_model_save_dir / paths["last_decoder_with_past_model_name"]

ort_inference_sessions = cls.load_model(
decoder_path=decoder_path,
Expand All @@ -481,11 +481,13 @@ def _from_pretrained(
except OSError:
logger.info("Generation config file not found, using a generation config created from the model config.")

onnx_paths = [decoder_without_past_path]
if decoder_merged_path is not None:
onnx_paths = []
if use_merged is False:
onnx_paths.append(decoder_without_past_path)
if use_cache is True:
onnx_paths.append(decoder_with_past_path)
else:
onnx_paths.append(decoder_merged_path)
if decoder_with_past_path is not None:
onnx_paths.append(decoder_with_past_path)

return cls(
ort_inference_sessions[0],
Expand Down
1 change: 1 addition & 0 deletions optimum/onnxruntime/modeling_ort.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,7 @@ def _prepare_io_binding(
for axis_name in output_node.shape:
output_shape.append(self._output_shape_inference(axis_name, dimensions))
output_buffer = self._prepare_output_buffer(model, output_shape, output_name)

io_binding.bind_output(
output_name,
output_buffer.device.type,
Expand Down
Loading