Add enhanced event tracking with TTFT measurement and compact serialization.#3253
Merged
lmcafee-nvidia merged 20 commits intoNVIDIA:mainfrom Feb 12, 2026
Merged
Add enhanced event tracking with TTFT measurement and compact serialization.#3253lmcafee-nvidia merged 20 commits intoNVIDIA:mainfrom
lmcafee-nvidia merged 20 commits intoNVIDIA:mainfrom
Conversation
Replace the single ADD event with three separate events to enable precise time-to-first-token measurement: - ADD_ENGINE: When request is added to engine via _add_request() - ADD_CONTEXT: When request is scheduled for prefill - FIRST_TOKEN: When first output token is about to be generated TTFT is now calculated as FIRST_TOKEN - ADD_ENGINE and included in the JSON output from gpt_dynamic_inference.py. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add tests/unit_tests/inference/test_utils.py with TestPriority enum for selective test execution based on priority levels - Add tests/unit_tests/inference/engines/test_dynamic_events.py with comprehensive tests for DynamicInferenceEvent and event lifecycle - Use consistent convention: CRITICAL=1, LOW=4 with skipif pattern TEST_PRIORITY < TestPriority.LEVEL Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Update test_events to use new event types (ADD_ENGINE, ADD_CONTEXT, FIRST_TOKEN) instead of old ADD type - Add test_event_timestamps integration test that verifies: - Completed requests have expected event sequence - Event timestamps are monotonically increasing - TTFT (FIRST_TOKEN - ADD_ENGINE) is positive - Total request time >= TTFT Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
kvareddy
approved these changes
Feb 5, 2026
tdene
reviewed
Feb 5, 2026
tdene
reviewed
Feb 5, 2026
tdene
reviewed
Feb 5, 2026
tdene
reviewed
Feb 5, 2026
tdene
reviewed
Feb 5, 2026
Refactor event tracking to use a unified GENERATED_TOKEN event that captures each token as it's generated, replacing the separate FIRST_TOKEN event and generated_tokens list. Changes: - Replace FIRST_TOKEN enum with GENERATED_TOKEN (payload = token id) - Remove generated_tokens field, add property that extracts from events - Replace add_event_first_token() with add_event_generated_token(token) - Update TTFT calculation to use first GENERATED_TOKEN timestamp - Update serialization to handle GENERATED_TOKEN payload - Update tests for new event structure Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Complete rewrite of test_dynamic_events.py with 81 tests across 12 test classes covering all 9 event types, payload validation, serialization, and lifecycle sequences. Test classes: - TestDynamicInferenceEventType: Verifies all 9 event types exist - TestEventCreation: Tests creating events with correct payloads - TestPayloadValidation: Tests payload validation rules - TestEventSerialization: Tests serialize/deserialize roundtrip - TestErrorPayloads: Tests error types as event payloads - TestRequestEventMethods: Tests all add_event_*() helper methods - TestGeneratedTokensProperty: Tests generated_tokens property - TestRequestSerialization: Tests request serialization with events - TestRequestRecordMerge: Tests DynamicInferenceRequestRecord - TestEventLifecycleSequences: Tests common event sequences - TestEventTimestamps: Tests timestamp behavior and TTFT - TestEventEdgeCases: Tests __str__, rapid events, many tokens Note: Includes workaround for DynamicInferenceRequest instantiation bug where generated_tokens property conflicts with inherited field. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add no-op setter for generated_tokens property to allow dataclass __init__ to work. The parent class InferenceRequest has generated_tokens as a field, which means the dataclass-generated __init__ tries to set it. Without a setter, this fails with "property has no setter" error. The setter ignores any value passed since generated_tokens is computed dynamically from GENERATED_TOKEN events. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Remove workaround helper function and use DynamicInferenceRequest directly now that the generated_tokens property bug is fixed. Also restore the original merge() tests that now work correctly. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Move add_event_finish() call inside post_process_requests() so it happens after the final GENERATED_TOKEN event is added, not before. This ensures the event sequence is: ADD_ENGINE -> ADD_CONTEXT -> GENERATED_TOKEN(s) -> FINISH Previously, FINISH was added before post_process_requests() was called, which meant FINISH came before the last generated token. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add --inference-dynamic-batching-track-generated-token-events CLI flag to control whether GENERATED_TOKEN events serialize as full dicts with timestamps or just as integer token IDs. When disabled (default), reduces serialization overhead by omitting timestamp data for token events. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Replace 89-test class-based suite with 10 substantial standalone tests that maximize coverage while reducing complexity. Remove TestPriority system. Each test covers multiple related behaviors in meaningful scenarios including all 9 event types, serialization, TTFT calculation, error handling, and complex multi-checkpoint lifecycles. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Resolve conflicts keeping event tracking features (track_generated_token_events, compact serialization, TTFT) while adopting main's improvements (NVTX profiling, _post_deserialize pattern, dict copy optimization). Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
No test files import TestPriority, so the module is dead code. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
tdene
reviewed
Feb 6, 2026
Address PR review: generated_tokens as a property computed from events was O(n) on every access (called every forward step). Restore it as a plain List[int] field, keep events as supplementary timestamp data, and have the track_generated_token_events flag control whether GENERATED_TOKEN events are created at all (rather than controlling serialization format). Also removes compact dict|int serialization mode entirely and fixes a missing nvtx range_pop() in DynamicInferenceEvent.serialize(). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
tdene
approved these changes
Feb 12, 2026
Comment on lines
177
to
179
| self.track_generated_token_events = getattr( | ||
| inference_config, 'track_generated_token_events', False | ||
| ) |
Contributor
There was a problem hiding this comment.
nit: I don't believe the getattr part is needed, since it's being added to inference_config.py
Contributor
Author
|
@shanmugamr1992 @Phlip79 , please review when you have time. thank you! |
shanmugamr1992
approved these changes
Feb 12, 2026
Also address PR review: replace getattr with direct attribute access for track_generated_token_events.
223793c to
76d49cb
Compare
Contributor
Author
|
/ok to test 76d49cb |
Contributor
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/21958742266 |
daiyaanarfeen
pushed a commit
to daiyaanarfeen/Megatron-LM
that referenced
this pull request
Feb 23, 2026
…zation. (NVIDIA#3253) Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do ?
Summary
This PR enhances the inference event tracking system with precise time-to-first-token (TTFT)
measurement, per-token event tracking, and configurable serialization modes.
Key improvements:
enable accurate TTFT measurement from request submission to first token generation
individual timestamps
--inference-dynamic-batching-track-generated-token-eventsflag to control whether token events serialize as full dicts (with timestamps) or just token IDs,
reducing overhead when timestamps aren't needed
covering event ordering, timestamps, serialization modes, and edge cases
Changes:
first_GENERATED_TOKEN.timestamp - ADD_ENGINE.timestamp--inference-dynamic-batching-track-generated-token-eventsfor verbose token eventtracking
Bug fixes:
Test plan
Contribution process
flowchart LR A[Pre-checks] --> B[PR Tests] subgraph Code Review/Approval C1[Expert Review] --> C2[Final Review] end B --> C1 C2 --> D[Merge]Pre-checks
Core 0.8)Code review
The following process is enforced via the CODEOWNERS file for changes into
megatron/core. For changes outside ofmegatron/core, it is up to the PR author whether or not to tag the Final Reviewer team.For MRs into `main` branch
Feel free to message or comment the @mcore-oncall to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!
(Step 1): Add PR label
Expert Review(Step 2): Collect the expert reviewers reviews
Expert Reviewlabel when your PR is ready for review.Final Review might get declined if these requirements are not fulfilled.
(Step 3): Final Review
Final Reviewlabel(Optional Step 4): Cherry-pick into release branch
If this PR also needs to be merged into
core_r*release branches, after this PR has been merged, selectCherry-pickto open a new PR into the release branch.For MRs into `dev` branch
The proposed review process for `dev` branch is under active discussion.MRs are mergable after one approval by either
eharper@nvidia.comorzijiey@nvidia.com.Merging your PR
Any member of core-adlr and
core-nemowill be able to merge your PR.