Skip to content

fix: token_dtype_code assigns uint16 to vocab sizes needing uint64 - #5561

Open
andrewwhitecdw wants to merge 3 commits into
NVIDIA-NeMo:mainfrom
andrewwhitecdw:bugfix/setup-token-dtype-code-assigns-uint16-to
Open

fix: token_dtype_code assigns uint16 to vocab sizes needing uint64#5561
andrewwhitecdw wants to merge 3 commits into
NVIDIA-NeMo:mainfrom
andrewwhitecdw:bugfix/setup-token-dtype-code-assigns-uint16-to

Conversation

@andrewwhitecdw

Copy link
Copy Markdown

This PR addresses the following issue in src/megatron/bridge/training/setup.py: token_dtype_code assigns uint16 to vocab sizes needing uint64.

Changes

  • src/megatron/bridge/training/setup.py: token_dtype_code assigns uint16 to vocab sizes needing uint64.

Details

--- a/src/megatron/bridge/training/setup.py
+++ b/src/megatron/bridge/training/setup.py
@@ -1,6 +1,4 @@
-    if hasattr(cfg.dataset, "token_dtype_code") and cfg.dataset.token_dtype_code is None:
-        vocab_size = getattr(tokenizer, "vocab_size", None)
-        if vocab_size is not None:
-            import numpy
-
-            cfg.dataset.token_dtype_code = 4 if vocab_size > numpy.iinfo(numpy.uint16).max + 1 else 8
+    if hasattr(cfg.dataset, "token_dtype_code") and cfg.dataset.token_dtype_code is None:
+        vocab_size = getattr(tokenizer, "vocab_size", None)
+        if vocab_size is not None:
+            cfg.dataset.token_dtype_code = _get_token_dtype_code(vocab_size)

Tests

  • tests/unit_tests/bridge/training/test_setup.py
--- a/tests/unit_tests/bridge/training/test_setup.py
+++ b/tests/unit_tests/bridge/training/test_setup.py
@@ -0,0 +1,22 @@
+import numpy as np
+import pytest
+
+from megatron.bridge.training.setup import _get_token_dtype_code
+
+
+def test_get_token_dtype_code_uint16():
+    """Vocab sizes up to 65536 fit in uint16 (numpy code 4)."""
+    assert _get_token_dtype_code(1) == np.dtype(np.uint16).num
+    assert _get_token_dtype_code(65535) == np.dtype(np.uint16).num
+    assert _get_token_dtype_code(65536) == np.dtype(np.uint16).num
+
+
+def test_get_token_dtype_code_uint64():
+    """Vocab sizes larger than 65536 require uint64 (numpy code 8)."""
+    assert _get_token_dtype_code(65537) == np.dtype(np.uint64).num
+    assert _get_token_dtype_code(100000) == np.dtype(np.uint64).num
+
+
+def test_get_token_dtype_code_boundary():
+    """The boundary between uint16 and uint64 is exactly 65536."""
+    assert _get_token_dtype_code(65536) == 4
+    assert _get_token_dtype_code(65537) == 8

Contributor guidelines

Per this repo's CONTRIBUTING.md:

  • All commits are signed off (Signed-off-by trailer, DCO).

Signed-off-by: andrewwhitecdw <andrewwhitecdw@users.noreply.github.com>
@copy-pr-bot

copy-pr-bot Bot commented Aug 13, 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.

@yaoyu-33 yaoyu-33 added bug Something isn't working area:data Dataset builders, preprocessing, and samplers needs-review PR is ready for code review and waiting on a reviewer labels Aug 13, 2026
@kamran-nvidia
kamran-nvidia requested a review from yaoyu-33 August 13, 2026 15:42
@yaoyu-33 yaoyu-33 added ready-to-merge PR is approved, current, and only waiting for CI to pass before merge and removed needs-review PR is ready for code review and waiting on a reviewer labels Aug 13, 2026
@kamran-nvidia

Copy link
Copy Markdown
Contributor

/ok to test f376614

@kamran-nvidia

Copy link
Copy Markdown
Contributor

@andrewwhitecdw Please address the CI failures

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

Labels

area:data Dataset builders, preprocessing, and samplers bug Something isn't working community-request ready-to-merge PR is approved, current, and only waiting for CI to pass before merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants