Skip to content

[Draft]Fake mode or CPU support - #410

Open
jiashuy wants to merge 3 commits into
NVIDIA:mainfrom
jiashuy:fake-mode
Open

[Draft]Fake mode or CPU support#410
jiashuy wants to merge 3 commits into
NVIDIA:mainfrom
jiashuy:fake-mode

Conversation

@jiashuy

@jiashuy jiashuy commented May 29, 2026

Copy link
Copy Markdown
Collaborator

Description

Checklist

  • I am familiar with the Contributing Guidelines.
  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

@jiashuy jiashuy changed the title Fake mode or CPU support [Draft]Fake mode or CPU support May 29, 2026
@greptile-apps

greptile-apps Bot commented May 29, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR introduces "fake mode" for DynamicEmb: a DYNAMICEMB_FAKE_MODE=1 runtime flag and a companion DYNAMICEMB_SKIP_CUDA_BUILD=1 install-time flag that together allow the package to be imported, planned, and run on CPU-only hosts. A new _fake.py module handles the extension stub, lazy fake-class construction, and a FakeBatchedDynamicEmbeddingTablesV2 subclass whose forward returns correctly shaped CPU zero-tensors wired into autograd.

  • _fake.py (616 lines): env-var reader, sys.modules stub for the missing C++ extension, and a lazily built fake subclass that replicates host-side bookkeeping while no-op'ing all GPU paths.
  • batched_dynamicemb_tables.py: adds a __new__ hook that redirects construction to the fake subclass only when the base class itself is instantiated, preserving explicit subclass paths.
  • Tests: attribute-parity test guards against drift between real and fake __init__, plus a full DMP sequence-mode forward/backward smoke test over a single-rank gloo group.

Confidence Score: 5/5

The change adds an opt-in CPU/fake path gated behind an env var; the real GPU path is not altered beyond the four-line new guard protected by a cls-identity check.

The real execution path is untouched except for the cls-identity-guarded new hook. Fake mode is entirely opt-in. Host-side bookkeeping methods are reused from the real class, keeping state in sync. No blocking issues found.

_fake.py warrants a second look on the _build_fake_class global cache (no lock) and the device-ID extraction already noted in a prior thread.

Important Files Changed

Filename Overview
corelib/dynamicemb/dynamicemb/_fake.py New 616-line module introducing fake-mode bootstrap, extension stub, and FakeBatchedDynamicEmbeddingTablesV2 subclass; well-structured but has a minor race condition in the global class cache.
corelib/dynamicemb/dynamicemb/batched_dynamicemb_tables.py Adds a concise new dispatch hook; the cls-identity guard is correct and preserves explicit subclass construction.
corelib/dynamicemb/dynamicemb/init.py Correctly places the bootstrap call before any submodule import; delete-after-call avoids polluting the public namespace.
corelib/dynamicemb/setup.py Adds DYNAMICEMB_SKIP_CUDA_BUILD opt-in with early-return from get_extensions(); straightforward and correct.
corelib/dynamicemb/test/unit_tests/test_fake_batched_dynamicemb_tables.py Comprehensive test suite covering dispatch, attribute parity, forward/backward shapes, DMP integration, and dump/load no-ops; uses a hardcoded port 29501 that can cause port conflicts during parallel CI runs.

Sequence Diagram

sequenceDiagram
    participant U as User Code
    participant Init as dynamicemb/__init__.py
    participant Fake as _fake.py
    participant V2 as BatchedDynamicEmbeddingTablesV2
    participant FV2 as FakeBatchedDynamicEmbeddingTablesV2

    Init->>Fake: bootstrap()
    U->>V2: "BatchedDynamicEmbeddingTablesV2(**kwargs)"
    V2->>Fake: maybe_get_fake_class()
    alt "FAKE_MODE=1"
        Fake-->>V2: FakeBatchedDynamicEmbeddingTablesV2
        V2-->>U: fake instance
        U->>FV2: forward(indices, offsets)
        FV2-->>U: zeros tensor [cpu]
    else "FAKE_MODE=0"
        Fake-->>V2: None
        V2-->>U: real instance
    end
Loading

Reviews (2): Last reviewed commit: "fake: disable use_index_dedup in DMP tes..." | Re-trigger Greptile

Comment on lines +387 to +391
if device is not None:
try:
self.device_id = int(str(device)[-1])
except ValueError:
self.device_id = 0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 The str(device)[-1] approach only reads the last character of the device string, so a device like cuda:10 yields "0" instead of 10, silently setting the wrong device ID. The except ValueError guard only catches the cpu case, not multi-digit indices. Use device.index (which is None for CPU) for a correct, idiomatic extraction.

Suggested change
if device is not None:
try:
self.device_id = int(str(device)[-1])
except ValueError:
self.device_id = 0
if device is not None:
d = torch.device(device) if not isinstance(device, torch.device) else device
self.device_id = d.index if d.index is not None else 0

Comment on lines 11 to 17
"test/unit_tests/test_alignment.sh"
"test/unit_tests/test_hash_roundrobin_kuairand.py"
"test/unit_tests/test_hybrid_storage_export.sh"
"test/unit_tests/test_vmm_tensor.py"
"test/unit_tests/test_fake_batched_dynamicemb_tables.sh"
)

LOAD_DUMP_TEST_FILES=(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Unexplained removal of hybrid-storage export test

test_hybrid_storage_export.sh is silently dropped from the CI suite and replaced by the new fake-mode test. If this test covered a code path that is still exercised in production (hybrid HBM/DDR storage with export), removing it from the suite is an untracked coverage gap. Was this intentional (e.g., the test is superseded, or it no longer compiles without GPU), and if so, can you add a brief comment or remove the file entirely?

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

jiashuy and others added 3 commits May 29, 2026 12:28
…KIP_CUDA_BUILD for nvcc-free install, plus tests covering dispatch / attribute parity / forward / DMP sequence-mode wiring.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
…st.sh

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
…ly kernel from dynamicemb_extensions, incompatible with the test's CPU input KJT).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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