Add VarlenNGramHashMapping for packed/varlen batching support in DeepSeek Engram - #32358
Open
kunal-vaishnavi with Copilot wants to merge 52 commits into
Open
kunal-vaishnavi with Copilot wants to merge 52 commits into
kunal-vaishnavi with Copilot wants to merge 52 commits into
Conversation
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
…e for NGramHashMapping/ShortConv (schema, CPU, CUDA) Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
…op docs Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
…ipliers check, fused CUDA reduction Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
T1: templatize the chunked NGramHashMapping equivalence test over int32/int64 so the WebGPU past_ids/present_ids state shaders get execution coverage. Q1: vectorize EngramGateScalarProgram, EngramGateProgram, ShortConvInvRmsProgram and ShortConvPresentStateProgram over the contiguous hidden dimension using GetMaxComponents + Flatten bindings + SumVector reductions. ShortConvProgram stays scalar because weight is laid out (channel, 1, kernel_size). Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
… CUDA grid/shared-mem, WebGPU empty input guard, negative-id tests - Reject non-positive vocab_sizes on CPU and document the requirement in the schema - Explain in the ShortConv doc why it is separate from CausalConvWithState - CPU ShortConv: allocate inv_rms via GetTempSpaceAllocator; hoist row base pointers - CUDA: raise GridSize cap from 65535 to the real 2^31-1 grid.x limit - CUDA: stage multipliers/vocab_sizes in shared memory, mark read-only pointers __restrict__ - WebGPU: do not bind zero-sized input_ids when sequence_length == 0 - Tests: negative ids/pad_id coverage for PositiveMod and WrappedMultiply, plus vocab_sizes rejection Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
…traints Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
…s and docs Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Agent-Logs-Url: https://github.com/microsoft/onnxruntime/sessions/780c3ba6-6eaf-48ed-9080-88cac877e87c Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Tianlei Wu (tianleiwu)
requested changes
Sep 4, 2026
Tianlei Wu (tianleiwu)
left a comment
Contributor
There was a problem hiding this comment.
The packed-boundary design and the follow-up offset-safety fixes look solid, and the multi-EP tests are strong. I found one blocking WebGPU bug when the optional present_ids output is omitted, plus one shape-inference validation gap for the same one-output form. Details are inline.
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
…ashmapping-for-ogaengine Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
…ashmapping-for-ogaengine Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Copilot started reviewing on behalf of
Ti-Tai Wang (titaiwangms)
September 9, 2026 19:07
View session
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Shape validation incorrectly depends on an optional output, and long prefills are serialized across all three implementations.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
- Files reviewed: 18/18 changed files
- Comments generated: 4
- Review effort level: Balanced
…ashmapping-for-ogaengine Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
…ashmapping-for-ogaengine Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Akshay Sonawane (apsonawane)
approved these changes
Sep 18, 2026
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.
Description
NGramHashMappingoperates on a dense(batch_size, sequence_length)layout and mixes n-gram hash inputs across positions within a row. Packing multiple sequences into one flat buffer (as required by ragged/packed batching engines likeOgaEngine) and reshaping to(1, num_tokens)would silently leak hash-mix inputs across sequence boundaries at every packed request boundary.This adds a new
com.microsoftop,VarlenNGramHashMapping, modeled onVarlenCausalConvWithState, which accepts acumulative_sequence_length(shape(batch_size + 1), device-resident int32) offsets tensor and clamps the n-gram window — andpast_ids/present_idshistory — at each packed request's own boundary instead of the flat buffer's.bert_defs.cc(registered inms_opset.h) taking token-major packedinput_ids((total_tokens)) pluscumulative_sequence_length;past_ids/present_idsare now indexed per-request instead of per fixed-stride batch row.ThreadPool::TryParallelFor; validates offsets host-side.VarlenCausalConvKernel); each block validates its own offsets before touching memory.present_ids.NGramHashMapping/EngramGate(from unmerged PR Add DeepSeek Engram contrib ops (EngramGate, NGramHashMapping) #32268) were not yet on this branch; selectively ported only theNGramHashMapping-related CPU/CUDA/WebGPU kernels,engram_helperfiles, and tests needed here (EngramGateand unrelatedCausalConvWithStatedilation/channels_last changes are out of scope).docs/ContribOperators.mdanddocs/OperatorKernels.mdto matchgen_docoutput.engram_ops_test.ccfor packed-vs-per-sequence equivalence, sequence-boundary no-leakage, packed chunked/decode equivalence across concurrent requests, negative ids, and invalidvocab_sizes.Motivation and Context
NGramHashMapping's dense(batch_size, sequence_length)layout is incompatible withOgaEngine's packed/ragged batching model (continuous batching / paged attention), where tokens from multiple in-flight requests are concatenated into a single flat, unpadded buffer. Without this change, models using DeepSeek Engram would silently corrupt n-gram hash ids at every sequence boundary whenever more than one request is batched together.