-
Notifications
You must be signed in to change notification settings - Fork 414
Support specifying input types in ONNX export script. #2084
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -108,6 +108,14 @@ def get_parser(): | |||||||||||||||||||||||||||||||
| You can specify --avg to use more checkpoints for model averaging.""", | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| parser.add_argument( | ||||||||||||||||||||||||||||||||
| "--use-int32-inputs", | ||||||||||||||||||||||||||||||||
| type=int, | ||||||||||||||||||||||||||||||||
| default=0, | ||||||||||||||||||||||||||||||||
| help="""1 to use int32_t as input types if applicable. 0 to use | ||||||||||||||||||||||||||||||||
| int64_t otherwise.""", | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
|
Comment on lines
+111
to
+117
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Restrict
Suggested fix parser.add_argument(
"--use-int32-inputs",
type=int,
default=0,
+ choices=(0, 1),
help="""1 to use int32_t as input types if applicable. 0 to use
int64_t otherwise.""",
)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| parser.add_argument( | ||||||||||||||||||||||||||||||||
| "--iter", | ||||||||||||||||||||||||||||||||
| type=int, | ||||||||||||||||||||||||||||||||
|
|
@@ -272,6 +280,7 @@ def export_encoder_model_onnx( | |||||||||||||||||||||||||||||||
| encoder_filename: str, | ||||||||||||||||||||||||||||||||
| opset_version: int = 11, | ||||||||||||||||||||||||||||||||
| dynamic_batch: bool = True, | ||||||||||||||||||||||||||||||||
| use_int32_inputs: int = 0, | ||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||||||||||||||||
| ) -> None: | ||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||
| Onnx model inputs: | ||||||||||||||||||||||||||||||||
|
|
@@ -307,6 +316,10 @@ def export_encoder_model_onnx( | |||||||||||||||||||||||||||||||
| x = torch.rand(1, T, 80, dtype=torch.float32) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| init_state = encoder_model.encoder.get_init_state() | ||||||||||||||||||||||||||||||||
| if use_int32_inputs: | ||||||||||||||||||||||||||||||||
| init_state = [ | ||||||||||||||||||||||||||||||||
| s if s.dtype != torch.int64 else s.to(torch.int32) for s in init_state | ||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| num_encoders = encoder_model.encoder.num_encoders | ||||||||||||||||||||||||||||||||
| logging.info(f"num_encoders: {num_encoders}") | ||||||||||||||||||||||||||||||||
|
|
@@ -409,6 +422,7 @@ def export_decoder_model_onnx( | |||||||||||||||||||||||||||||||
| decoder_filename: str, | ||||||||||||||||||||||||||||||||
| opset_version: int = 11, | ||||||||||||||||||||||||||||||||
| dynamic_batch: bool = True, | ||||||||||||||||||||||||||||||||
| use_int32_inputs: int = 0, | ||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||||||||||||||||
| ) -> None: | ||||||||||||||||||||||||||||||||
| """Export the decoder model to ONNX format. | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
@@ -432,7 +446,10 @@ def export_decoder_model_onnx( | |||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||
| context_size = decoder_model.decoder.context_size | ||||||||||||||||||||||||||||||||
| vocab_size = decoder_model.decoder.vocab_size | ||||||||||||||||||||||||||||||||
| y = torch.zeros(1, context_size, dtype=torch.int64) | ||||||||||||||||||||||||||||||||
| if use_int32_inputs: | ||||||||||||||||||||||||||||||||
| y = torch.zeros(1, context_size, dtype=torch.int32) | ||||||||||||||||||||||||||||||||
|
Comment on lines
+449
to
+450
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While setting |
||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||
| y = torch.zeros(1, context_size, dtype=torch.int64) | ||||||||||||||||||||||||||||||||
| decoder_model = torch.jit.script(decoder_model) | ||||||||||||||||||||||||||||||||
| torch.onnx.export( | ||||||||||||||||||||||||||||||||
| decoder_model, | ||||||||||||||||||||||||||||||||
|
|
@@ -655,6 +672,7 @@ def main(): | |||||||||||||||||||||||||||||||
| encoder_filename, | ||||||||||||||||||||||||||||||||
| opset_version=opset_version, | ||||||||||||||||||||||||||||||||
| dynamic_batch=params.dynamic_batch == 1, | ||||||||||||||||||||||||||||||||
| use_int32_inputs=params.use_int32_inputs, | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
| logging.info(f"Exported encoder to {encoder_filename}") | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
@@ -665,6 +683,7 @@ def main(): | |||||||||||||||||||||||||||||||
| decoder_filename, | ||||||||||||||||||||||||||||||||
| opset_version=opset_version, | ||||||||||||||||||||||||||||||||
| dynamic_batch=params.dynamic_batch == 1, | ||||||||||||||||||||||||||||||||
| use_int32_inputs=params.use_int32_inputs, | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
| logging.info(f"Exported decoder to {decoder_filename}") | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
str2boolinstead ofintfor--use-int32-inputsto be consistent with other boolean flags in the repository (like--use-averaged-model) and to provide a more user-friendly command-line interface.