Skip to content

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

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

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
Contributor

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).

@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 previously approved these changes Aug 13, 2026
@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

@kamran-nvidia

Copy link
Copy Markdown
Contributor

/ok to test efc04ca

@andrewwhitecdw
andrewwhitecdw force-pushed the bugfix/setup-token-dtype-code-assigns-uint16-to branch from f62d392 to 1f0fa43 Compare August 18, 2026 00:58
Signed-off-by: andrewwhitecdw <andrewwhitecdw@users.noreply.github.com>
@andrewwhitecdw
andrewwhitecdw force-pushed the bugfix/setup-token-dtype-code-assigns-uint16-to branch from 1f0fa43 to 857c8f1 Compare August 18, 2026 00:58
@kamran-nvidia

Copy link
Copy Markdown
Contributor

/ok to test 857c8f1

@adityavavreNVDA

Copy link
Copy Markdown
Contributor

/ok to test 548cad2

@adityavavreNVDA adityavavreNVDA removed the ready-to-merge PR is approved, current, and only waiting for CI to pass before merge label Aug 20, 2026
@cuichenx

Copy link
Copy Markdown
Contributor

The original code is correct, no fix needed https://github.com/NVIDIA/Megatron-LM/blob/4da7da91a67f02a3b30b140ca0d160d3d3fcfdc9/megatron/core/datasets/indexed_dataset.py#L53-L60

This branch was previously deployed

2 inactive deployments
public — 548cad22 Deployed Aug 20, 2026 by copy-pr-bot[bot] via release / finalize / publish-docs #4861
test — 548cad22 Deployed Aug 20, 2026 by copy-pr-bot[bot] via cicd-wait-in-queue #20836
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants