Skip to content

Comments

Fix Nanocodec's FSQ codes function: tensor creation misplacement#15405

Open
Ferdydh wants to merge 1 commit intoNVIDIA-NeMo:mainfrom
Ferdydh:main
Open

Fix Nanocodec's FSQ codes function: tensor creation misplacement#15405
Ferdydh wants to merge 1 commit intoNVIDIA-NeMo:mainfrom
Ferdydh:main

Conversation

@Ferdydh
Copy link

@Ferdydh Ferdydh commented Feb 17, 2026

Bug: torch.arange always creates a tensor on cpu, but the FSQ might be moved to another device, crashing codes function when called (because of self.decode function: indices//self.dim_base_index...)

Fix: just move to device when creating tensor.

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 ?

Add a one line overview of what this PR aims to accomplish.

Collection: [Note which collection this PR will affect]

Changelog

  • Add specific line by line info of high level changes in this PR.

Usage

  • You can potentially add a usage example below
# Add a code snippet demonstrating how to use this 

GitHub Actions CI

The Jenkins CI system has been replaced by GitHub Actions self-hosted runners.

The GitHub Actions CI will run automatically when the "Run CICD" label is added to the PR.
To re-run CI remove and add the label again.
To run CI on an untrusted fork, a NeMo user with write access must first click "Approve and run".

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)
    • Reviewer: Does the PR have correct import guards for all optional libraries?

PR Type:

  • New Feature
  • Bugfix
  • Documentation

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)

Bug: torch.arange always creates a tensor on cpu, but the FSQ might be moved to another device, crashing codes function when called (because of self.decode function: indices//self.dim_base_index...)

Fix: just move to device when creating tensor.


Signed-off-by: Ferdydh <ferdydh.hadiwijaya@tum.de>
@github-actions github-actions bot added the TTS label Feb 17, 2026
@pzelasko
Copy link
Collaborator

CC @rlangman @Edresson

@blisc
Copy link
Collaborator

blisc commented Feb 20, 2026

Most of the properties of this class return native python types not tensors. @rlangman, what is self.codes supposed to return and should it be a torch.tensor?

@rlangman
Copy link
Collaborator

Most of the properties of this class return native python types not tensors. @rlangman, what is self.codes supposed to return and should it be a torch.tensor?

self.codes and self.codebook return a map of the implicit FSQ codebook. For example if the FSQ has levels [5, 5, 5] with codebooks size 5^3=125, then self.codes returns a tensor of size [125, 3] with codes[0] = [0, 0, 0], codes[1] =[0, 0, 0.5], etc.

From what I remember these functions are just there to help people visualize what the codebook structure looks like. They would never be used during training or inference.

The reason this can throw an error is because the decode() method uses self.dim_base_index and self.num_levels which are saved as buffers. Even if we returned the value as a native type, it would still encounter this problem when calculating it. In retrospect I think these fields should have been constants instead of buffers/model parameters. It would also work if self.codes were computed once at initialization and saved as a normal class field instead of as an @property.

The way it is implemented right now, I think the change here is appropriate.

@pzelasko pzelasko enabled auto-merge (squash) February 20, 2026 17:29
@chtruong814 chtruong814 added the needs-follow-up Issue needs follow-up label Feb 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants