Skip to content

Fix wav2vec2 ONNX export when layer drop is disabled - #4214

Open
saisharan0103 wants to merge 1 commit into
pytorch:mainfrom
saisharan0103:mergepath/issue-4116-wav2vec2model-can-t-be-exported-with
Open

Fix wav2vec2 ONNX export when layer drop is disabled#4214
saisharan0103 wants to merge 1 commit into
pytorch:mainfrom
saisharan0103:mergepath/issue-4116-wav2vec2model-can-t-be-exported-with

Conversation

@saisharan0103

Copy link
Copy Markdown

Fixes #4116.

Wav2Vec2Model ONNX export could fail for freshly constructed wav2vec2_xlsr_300m models because they are in training mode by default while encoder_layer_drop is 0.0. In that state, Transformer.forward still evaluated torch.rand(1).item() before deciding whether to skip a layer, so torch.onnx.export(..., dynamo=True) encountered Python scalar extraction from a random tensor even though layer dropping was disabled.

This changes the layer-drop gate to short-circuit before creating a random tensor when the module is not training or when self.layer_drop is 0.0, while preserving stochastic layer skipping for positive layer-drop values during training.

A focused regression test was added for ONNX export of a very small wav2vec2_model configured with encoder_layer_drop=0.0 and left in training mode, guarded for the optional ONNX export dependencies.

ruff check src/torchaudio/models/wav2vec2/components.py test/torchaudio_unittest/models/wav2vec2/model_test.py reports no new findings on the changed files.
Ran pytest -x locally with no new failures.

@saisharan0103
saisharan0103 requested a review from a team as a code owner August 2, 2026 19:14
@pytorch-bot

pytorch-bot Bot commented Aug 2, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/audio/4214

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla

meta-cla Bot commented Aug 2, 2026

Copy link
Copy Markdown

Hi @saisharan0103!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wav2Vec2Model can't be exported with torch.onnx.export()

1 participant