Pewarta Proxy

📋 Server Request & Proxy Logs

Loading active proxy settings...
Developed by Khairul Adha (adhexa.id)
Mengambil log server...

⚙️ Pengaturan Proxy Target

Skip to content

Keep dtype and device in TextEncoder.prepend_bos_index / append_eos_index for tensor input - #3108

Open
sungbin1015 wants to merge 1 commit into
speechbrain:developfrom
sungbin1015:fix/text-encoder-bos-eos-dtype
Open

sungbin1015 wants to merge 1 commit into
speechbrain:developfrom
sungbin1015:fix/text-encoder-bos-eos-dtype

Conversation

@sungbin1015

Copy link
Copy Markdown
Contributor

What does this PR do?

When TextEncoder.prepend_bos_index / append_eos_index get a tensor, the BOS/EOS index is built with torch.Tensor([...]), which is float32 on the CPU. torch.cat then promotes the whole result to float, so encoded token ids come back as floats:

import torch
from speechbrain.dataio.encoder import TextEncoder

encoder = TextEncoder()
encoder.update_from_iterable([["a", "b"]])
encoder.add_bos_eos()
tokens = torch.LongTensor(encoder.encode_sequence(["a", "b"]))
print(encoder.prepend_bos_index(tokens))  # tensor([2., 0., 1.])
print(encoder.append_eos_index(tokens))   # tensor([0., 1., 3.])

Feeding that into an nn.Embedding fails with "Expected tensor for argument #1 'indices' to have one of the following scalar types: Long, Int", which is presumably why the integration tests and g2p/dataio.py call .long() on the result. The index tensor is also always created on the CPU, so a CUDA input should hit a device mismatch in torch.cat (I only have a CPU box, so I checked that with a meta tensor, not a GPU). I switched both to x.new_tensor(...) so the result keeps the input's dtype and device, and added a test that uses the device fixture. I left the existing .long() calls alone; they're harmless now.

No breaking changes unless someone relied on getting floats back. The encoder tests, its doctests and the ASR_seq2seq integration test pass locally (CPU only).

Didn't find this reported; the only other open PR touching encoder.py (#3102) just carries the limited_labelset_from_iterable change from #3095.

Before submitting
  • Did you read the contributor guideline?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
  • Does your code adhere to project-specific code style and conventions?

PR review

Reviewer checklist
  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified
  • Confirm that the changes adhere to compatibility requirements (e.g., Python version, platform)
  • Review the self-review checklist to ensure the code is ready for review

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.

1 participant