Skip to content

feat: Add.generate() function with KV cache support for Nemotron v3 - #1332

Merged
akoumpa merged 3 commits into
mainfrom
feat/nemotron-generate-support
Feb 24, 2026
Merged

akoumpa merged 3 commits into
mainfrom
feat/nemotron-generate-support

Conversation

@pzelasko

Copy link
Copy Markdown
Contributor

What does this PR do ?

Add.generate() function with KV cache support for Nemotron v3.

Changelog

  • Add.generate() function with KV cache support for Nemotron v3

Before your PR is "Ready for review"

Pre checks:

  • Make sure you read and followed Contributor guidelines
  • Did you write any new necessary tests?
  • Did you add or update any necessary documentation?

If you haven't finished some of the above items you can still open "Draft" PR.

Additional Information

  • Related to # (issue)

pzelasko and others added 2 commits February 18, 2026 12:17
Adds GenerationMixin to NemotronHForCausalLM so the model exposes the
standard HuggingFace .generate() API (greedy, beam search, sampling, etc.).

Key changes:
- Inherit from transformers.generation.GenerationMixin
- Set _is_stateful=True to prevent DynamicCache creation (hybrid
  Mamba2/Attention architecture cannot use a standard KV cache)
- Add device property required by GenerationMixin
- Initialize self.generation_config in __init__
- Update forward() to return CausalLMOutputWithPast (logits, optional
  loss, past_key_values=None) and accept all standard CausalLM params
  (inputs_embeds, labels, use_cache, cache_position, position_ids,
  logits_to_keep)
- Override prepare_inputs_for_generation() to pass the full accumulated
  sequence each step (no cache slicing; correct but O(n) per token)
- Update tests to use output.logits instead of raw tensor return value
- Add 7 new tests covering GenerationMixin integration and .generate()

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…orCausalLM

Add NemotronHybridCache managing attention KV cache and Mamba2 conv/SSM
state, enabling efficient autoregressive generation. Mamba2 mixer now
supports three code paths: fused training, unfused prefill with cache
init, and single-step decode with state update.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@copy-pr-bot

copy-pr-bot Bot commented Feb 18, 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.

@akoumpa akoumpa changed the title Add.generate() function with KV cache support for Nemotron v3 feat: Add.generate() function with KV cache support for Nemotron v3 Feb 19, 2026
@akoumpa

akoumpa commented Feb 19, 2026

Copy link
Copy Markdown
Contributor

/ok to test d7838bd

@pzelasko
pzelasko marked this pull request as ready for review February 19, 2026 20:26
Signed-off-by: Piotr Żelasko <pzelasko@nvidia.com>
@adil-a adil-a linked an issue Feb 22, 2026 that may be closed by this pull request
@akoumpa

akoumpa commented Feb 24, 2026

Copy link
Copy Markdown
Contributor

/ok to test d22231b

@akoumpa
akoumpa enabled auto-merge (squash) February 24, 2026 16:03
@akoumpa
akoumpa merged commit d2b2e6a into main Feb 24, 2026
51 checks passed
@akoumpa
akoumpa deleted the feat/nemotron-generate-support branch February 24, 2026 16:04
akoumpa pushed a commit that referenced this pull request Feb 25, 2026
…#1332)

* feat: add .generate() support to NemotronHForCausalLM

Adds GenerationMixin to NemotronHForCausalLM so the model exposes the
standard HuggingFace .generate() API (greedy, beam search, sampling, etc.).

Key changes:
- Inherit from transformers.generation.GenerationMixin
- Set _is_stateful=True to prevent DynamicCache creation (hybrid
  Mamba2/Attention architecture cannot use a standard KV cache)
- Add device property required by GenerationMixin
- Initialize self.generation_config in __init__
- Update forward() to return CausalLMOutputWithPast (logits, optional
  loss, past_key_values=None) and accept all standard CausalLM params
  (inputs_embeds, labels, use_cache, cache_position, position_ids,
  logits_to_keep)
- Override prepare_inputs_for_generation() to pass the full accumulated
  sequence each step (no cache slicing; correct but O(n) per token)
- Update tests to use output.logits instead of raw tensor return value
- Add 7 new tests covering GenerationMixin integration and .generate()

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat: add KV cache support for O(1) per-step generation in NemotronHForCausalLM

Add NemotronHybridCache managing attention KV cache and Mamba2 conv/SSM
state, enabling efficient autoregressive generation. Mamba2 mixer now
supports three code paths: fused training, unfused prefill with cache
init, and single-step decode with state update.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* Fix linter

Signed-off-by: Piotr Żelasko <pzelasko@nvidia.com>

---------

Signed-off-by: Piotr Żelasko <pzelasko@nvidia.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
linnanwang pushed a commit that referenced this pull request Apr 24, 2026
…#1332)

* feat: add .generate() support to NemotronHForCausalLM

Adds GenerationMixin to NemotronHForCausalLM so the model exposes the
standard HuggingFace .generate() API (greedy, beam search, sampling, etc.).

Key changes:
- Inherit from transformers.generation.GenerationMixin
- Set _is_stateful=True to prevent DynamicCache creation (hybrid
  Mamba2/Attention architecture cannot use a standard KV cache)
- Add device property required by GenerationMixin
- Initialize self.generation_config in __init__
- Update forward() to return CausalLMOutputWithPast (logits, optional
  loss, past_key_values=None) and accept all standard CausalLM params
  (inputs_embeds, labels, use_cache, cache_position, position_ids,
  logits_to_keep)
- Override prepare_inputs_for_generation() to pass the full accumulated
  sequence each step (no cache slicing; correct but O(n) per token)
- Update tests to use output.logits instead of raw tensor return value
- Add 7 new tests covering GenerationMixin integration and .generate()

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat: add KV cache support for O(1) per-step generation in NemotronHForCausalLM

Add NemotronHybridCache managing attention KV cache and Mamba2 conv/SSM
state, enabling efficient autoregressive generation. Mamba2 mixer now
supports three code paths: fused training, unfused prefill with cache
init, and single-step decode with state update.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* Fix linter

Signed-off-by: Piotr Żelasko <pzelasko@nvidia.com>

---------

Signed-off-by: Piotr Żelasko <pzelasko@nvidia.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
kashif pushed a commit to kashif/Automodel that referenced this pull request May 25, 2026
…NVIDIA-NeMo#1332)

* feat: add .generate() support to NemotronHForCausalLM

Adds GenerationMixin to NemotronHForCausalLM so the model exposes the
standard HuggingFace .generate() API (greedy, beam search, sampling, etc.).

Key changes:
- Inherit from transformers.generation.GenerationMixin
- Set _is_stateful=True to prevent DynamicCache creation (hybrid
  Mamba2/Attention architecture cannot use a standard KV cache)
- Add device property required by GenerationMixin
- Initialize self.generation_config in __init__
- Update forward() to return CausalLMOutputWithPast (logits, optional
  loss, past_key_values=None) and accept all standard CausalLM params
  (inputs_embeds, labels, use_cache, cache_position, position_ids,
  logits_to_keep)
- Override prepare_inputs_for_generation() to pass the full accumulated
  sequence each step (no cache slicing; correct but O(n) per token)
- Update tests to use output.logits instead of raw tensor return value
- Add 7 new tests covering GenerationMixin integration and .generate()

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat: add KV cache support for O(1) per-step generation in NemotronHForCausalLM

Add NemotronHybridCache managing attention KV cache and Mamba2 conv/SSM
state, enabling efficient autoregressive generation. Mamba2 mixer now
supports three code paths: fused training, unfused prefill with cache
init, and single-step decode with state update.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* Fix linter

Signed-off-by: Piotr Żelasko <pzelasko@nvidia.com>

---------

Signed-off-by: Piotr Żelasko <pzelasko@nvidia.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>

This branch was previously deployed

2 inactive deployments
nemo-ci — d22231b7 Deployed Feb 24, 2026 by copy-pr-bot[bot] via L2_HF_PEFT #4044
test — d22231b7 Deployed Feb 24, 2026 by copy-pr-bot[bot] via cicd-wait-in-queue #4044
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.

Support .generate() for Nemotron Nano V3 custom re-implementation

2 participants