Skip to content

Fix random_segment TypeError when duration_sec is a float - #16303

Open
SIDDARTHAREDDY8 wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
SIDDARTHAREDDY8:issue-16104-random-segment-float
Open

SIDDARTHAREDDY8 wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
SIDDARTHAREDDY8:issue-16104-random-segment-float

Conversation

@SIDDARTHAREDDY8

Copy link
Copy Markdown

Important

The Update branch button must only be pressed in very rare occassions.
An outdated branch is never blocking the merge of a PR.
Please reach out to the automation team before pressing that button.

What does this PR do ?

Fixes RandomSegmentPerturbation (random_segment augmentation) crashing with TypeError: pad_width must be of integral type. whenever it has to pad a short utterance and duration_sec is a float - including the constructor default 32.0.

Collection: ASR

Changelog

  • nemo/collections/asr/parts/preprocessing/perturb.py: compute pad_size as math.ceil(self._duration_sec * data.sample_rate) - data.num_samples, an integer number of samples, so AudioSegment.pad -> numpy.pad always receives an integral pad_width. math.ceil rather than round/truncation is deliberate: the next statement is subsegment(0.0, duration_sec), which raises ValueError if the segment ends up shorter than duration_sec, so the padded length must not round down. The integer-duration_sec path is unchanged (math.ceil of an int is a no-op).
  • tests/collections/asr/test_preprocessing_segment.py: new TestRandomSegmentPerturbation with parametrized duration_sec in [32.0, 32, 16.0, 16] verifying a short utterance is zero-padded to exactly int(duration_sec) * sample_rate samples for both int and float spellings.

Usage

import numpy as np
from nemo.collections.asr.parts.preprocessing.perturb import RandomSegmentPerturbation
from nemo.collections.asr.parts.preprocessing.segment import AudioSegment

seg = AudioSegment(np.zeros(16000 * 5, dtype=np.float32), 16000)  # 5 s of audio
p = RandomSegmentPerturbation(duration_sec=32.0, pad_to_duration=True)  # class defaults
p.perturb(seg)
print(len(seg.samples))  # 512000 (previously: TypeError)

Before your PR is "Ready for review"

Pre checks:

  • Make sure you read and followed Contributor guidelines
  • Did you write any new necessary tests?
  • Did you add or update any necessary documentation?
  • Does the PR affect components that are optional to install? (Ex: Numba, Pynini, Apex etc)

PR Type:

  • New Feature
  • Bugfix
  • Documentation

Additional Information

Signed-off-by: SIDDARTHA REDDY <75976672+SIDDARTHAREDDY8@users.noreply.github.com>
@copy-pr-bot

copy-pr-bot Bot commented Sep 27, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions github-actions Bot added the ASR label Sep 27, 2026
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the waiting-on-maintainers Waiting on maintainers to respond label Sep 29, 2026

This branch has not been deployed

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

Labels

ASR community-request waiting-on-maintainers Waiting on maintainers to respond

2 participants