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

onnx export for G2P models #8177

Merged
merged 20 commits into from
Mar 20, 2024
Merged

onnx export for G2P models #8177

merged 20 commits into from
Mar 20, 2024

Conversation

ppisljar
Copy link
Contributor

@ppisljar ppisljar commented Jan 16, 2024

What does this PR do ?

Adds support for ONNX export to G2P models (CTC/byT5)

Collection: tts

Jenkins CI

To run Jenkins, a NeMo User with write access must comment jenkins on the PR.

Before your PR is "Ready for review"

Pre checks:

PR Type:

  • New Feature

If you haven't finished some of the above items you can still open "Draft" PR.

Who can review?

Anyone in the community is free to review the PR once the checks have passed.
Contributor guidelines contains specific people who can review PRs to various areas.

Additional Information

  • Related to # (issue)

@github-actions github-actions bot added the TTS label Jan 16, 2024
@ppisljar
Copy link
Contributor Author

Hello,

i am trying to make G2P models exportable, but i am running into issues with ONNX export.
I hope someone here can guide me to the correct solution.

@ppisljar
Copy link
Contributor Author

both (t5 and ctc) return this error:
i am guessing i am doing something wrong with the output types ?

Traceback (most recent call last):
  File "/workspace/6_export.py", line 38, in <module>
    model.export('mymodel.onnx')
  File "/usr/local/lib/python3.10/dist-packages/nemo/core/classes/exportable.py", line 113, in export
    out, descr, out_example = model._export(
  File "/usr/local/lib/python3.10/dist-packages/nemo/core/classes/exportable.py", line 220, in _export
    torch.onnx.export(
  File "/usr/local/lib/python3.10/dist-packages/torch/onnx/utils.py", line 516, in export
    _export(
  File "/usr/local/lib/python3.10/dist-packages/torch/onnx/utils.py", line 1582, in _export
    graph, params_dict, torch_out = _model_to_graph(
  File "/usr/local/lib/python3.10/dist-packages/torch/onnx/utils.py", line 1182, in _model_to_graph
    _C._jit_pass_onnx_assign_output_shape(
RuntimeError: ArrayRef: invalid index Index = 0; Length = 0

ppisljar and others added 3 commits January 16, 2024 07:55
makes G2P ctc model exportable

Signed-off-by: Peter Pisljar <peter.pisljar@gmail.com>
Signed-off-by: ppisljar <peter.pisljar@gmail.com>
Signed-off-by: ppisljar <peter.pisljar@gmail.com>
for more information, see https://pre-commit.ci

Signed-off-by: ppisljar <peter.pisljar@gmail.com>
@ppisljar
Copy link
Contributor Author

i was able to get onnx export to finish (with a lot of warnings) for CTC model, if i changed the last lines to :

        preds_str, _ = self.decoding.ctc_decoder_predictions_tensor(
            log_probs, decoder_lengths=encoded_len, return_hypotheses=True
        )
        results = [h.y_sequence for h in preds_str]
        return tuple(results)

as it seems that returning a string was an issue ?

however when i try to use this onnx model, it tells me it has 0 inputs:

ort_session = onnxruntime.InferenceSession("mymodel.onnx", providers=["CPUExecutionProvider"])
print(ort_session.get_inputs())

returns []

@ppisljar
Copy link
Contributor Author

in the latest commit e93ffec ctc decoding is skipped, in this way export works succesfully for CTC model as well.

is it not possible to make ctc decoding part of the onnx model ?

ppisljar and others added 10 commits January 16, 2024 13:15
Signed-off-by: ppisljar <peter.pisljar@gmail.com>
for more information, see https://pre-commit.ci

Signed-off-by: ppisljar <peter.pisljar@gmail.com>
Signed-off-by: ppisljar <peter.pisljar@gmail.com>
for more information, see https://pre-commit.ci

Signed-off-by: ppisljar <peter.pisljar@gmail.com>
Signed-off-by: ppisljar <peter.pisljar@gmail.com>
for more information, see https://pre-commit.ci

Signed-off-by: ppisljar <peter.pisljar@gmail.com>
Signed-off-by: ppisljar <peter.pisljar@gmail.com>
for more information, see https://pre-commit.ci

Signed-off-by: ppisljar <peter.pisljar@gmail.com>
Signed-off-by: ppisljar <peter.pisljar@gmail.com>
for more information, see https://pre-commit.ci

Signed-off-by: ppisljar <peter.pisljar@gmail.com>
@ppisljar ppisljar marked this pull request as ready for review January 16, 2024 13:21
@ppisljar
Copy link
Contributor Author

i verified the generated onnx models and they seem to work correctly.

still i would really appreciate if someone can guide to towards making ctc decoding part of the nemo model as well as without it its quite hard to consume it.

@ekmb ekmb requested a review from mgrafu January 16, 2024 23:17
@ppisljar
Copy link
Contributor Author

@rlangman can you take a look ?

@mgrafu
Copy link
Collaborator

mgrafu commented Jan 29, 2024

@borisfom could you take a look at this?

@mgrafu mgrafu requested a review from borisfom January 29, 2024 19:46
Copy link
Collaborator

@rlangman rlangman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a few questions, but it would be nice to hear from someone who is more familiar with ONNX and these particular models.

nemo/collections/tts/g2p/models/ctc.py Show resolved Hide resolved
def output_types(self):
return self._output_types

def input_example(self, max_batch=1, max_dim=44):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like max_batch and max_dim are not used. Are they required by the export code?

nemo/collections/tts/g2p/models/t5.py Show resolved Hide resolved
borisfom
borisfom previously approved these changes Feb 1, 2024
@borisfom
Copy link
Collaborator

borisfom commented Feb 1, 2024

@ppisljar @rlangman : For ASR models. it's typical to exclude CTC decoder and tokenizer from ONNX export - as they normally could not be exported. So only DNN part (EncDecModel) is usually represented by ONNX graph.


def forward_for_export(self, input_ids):
outputs = self.model.generate(
input_ids, output_scores=True, return_dict_in_generate=True, max_length=model_max_target_len
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would max_length=model_max_target_len be max_length=self.max_target_len ?

Copy link
Contributor

This PR is stale because it has been open for 14 days with no activity. Remove stale label or comment or update or this will be closed in 7 days.

@github-actions github-actions bot added the stale label Feb 20, 2024
Signed-off-by: Peter Pisljar <peter.pisljar@gmail.com>
Signed-off-by: Peter Pisljar <peter.pisljar@gmail.com>
@github-actions github-actions bot removed the stale label Feb 27, 2024
@ekmb
Copy link
Collaborator

ekmb commented Mar 5, 2024

jenkins

@ekmb
Copy link
Collaborator

ekmb commented Mar 5, 2024

@mgrafu , @borisfom are you OK with the changes?

@ekmb
Copy link
Collaborator

ekmb commented Mar 12, 2024

jenkins

@ppisljar
Copy link
Contributor Author

ping

@ekmb
Copy link
Collaborator

ekmb commented Mar 18, 2024

jenkins

@ekmb
Copy link
Collaborator

ekmb commented Mar 18, 2024

jenkins

1 similar comment
@ekmb
Copy link
Collaborator

ekmb commented Mar 20, 2024

jenkins

@ekmb ekmb merged commit 3818e42 into NVIDIA:main Mar 20, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants