Skip to content

model: support for LlamaBidirectionalModel architecture - #18220

Merged
CISC merged 4 commits into
ggml-org:masterfrom
sfallah:sf/llama-embed-nemotron
Dec 24, 2025
Merged

CISC merged 4 commits into
ggml-org:masterfrom
sfallah:sf/llama-embed-nemotron

Conversation

@sfallah

@sfallah sfallah commented Dec 20, 2025 •

Copy link
Copy Markdown
Contributor

This PR adds support for LlamaBidirectionalModel architectures, resolving feature request #17478.

The implementation enables bidirectional LLaMA embedding models—such as:

  • nvidia/llama-embed-nemotron-8b
  • nvidia/llama-nemotron-embed-1b-v2

while keeping changes minimal by reusing/copying the existing implementation of LLAMA wherever possible.


Key Points

  • Support for LlamaBidirectionalModel
  • Minimal changes: the implementation largely copies the existing LLaMA architecture already present
  • this copies and adapts the existiting model implementation in src/models/llama.cpp to handle bidirectional attention required for embeddings

Validation

  • Successfully tested with:
    • nvidia/llama-embed-nemotron-8b
    • nvidia/llama-nemotron-embed-1b-v2
  • Embedding outputs have the expected shape and value ranges compared to reference implementations

GGUF Models

sabafallah/llama-nemotron-embed-1b-v2-GGUF
sabafallah/llama-embed-nemotron-8b-GGUF

Tests

llama-nemotron-embed-1b-v2

curl http://localhost:8080/v1/embeddings \
-H "Content-Type: application/json" \
-H "Authorization: Bearer no-key" \
-d '{
  "model": "llama-nemotron-embed-1b-v2-Q4_K_M.gguf",
  "input": [
    "query: How do neural networks learn patterns from examples?",
    "passage: Deep learning models adjust their weights through backpropagation, using gradient descent to minimize error on training data and improve predictions over time.",
    "passage: Market prices are determined by the relationship between how much people want to buy a product and how much is available for sale, with scarcity driving prices up and abundance driving them down."
  ]
}'

llama-nemotron-embed-1b-v2

curl http://localhost:8080/v1/embeddings \
-H "Content-Type: application/json" \
-H "Authorization: Bearer no-key" \
-d '{
  "model": "llama-embed-nemotron-8b-Q4_K_M.gguf",
  "input": [
    "Instruct: Given a question, retrieve passages that answer the question\nQuery: How do neural networks learn patterns from examples?",
    "Deep learning models adjust their weights through backpropagation, using gradient descent to minimize error on training data and improve predictions over time.",
    "Market prices are determined by the relationship between how much people want to buy a product and how much is available for sale, with scarcity driving prices up and abundance driving them down."
  ]
}'

@github-actions github-actions Bot added model Model specific python python script changes labels Dec 20, 2025
@sfallah
sfallah marked this pull request as ready for review December 22, 2025 12:46
@sfallah sfallah changed the title model: support nvidia/llama-embed-nemotron model: support for LlamaBidirectionalModel architecture Dec 22, 2025
Comment thread src/llama-arch.cpp Resolved
@sfallah
sfallah requested a review from CISC December 22, 2025 17:38
@CISC

CISC commented Dec 22, 2025

Copy link
Copy Markdown
Member

Instead of duplicating the model code, perhaps just template it as for non/swa models, just with/out cache and output.

Comment thread convert_hf_to_gguf.py
raise ValueError(f"Unprocessed experts: {experts}")


@ModelBase.register("LlamaBidirectionalModel")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Have you tested if adding LlamaBidirectionalForSequenceClassification too just works?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have not tested it, but I have had a look at nvidia/llama-nemotron-rerank-1b-v2.
I think the classifier tensor (score.weight) need to be added for it to work.

@CISC
CISC merged commit 54132f1 into ggml-org:master Dec 24, 2025
ppaleja pushed a commit to ppaleja/llama.cpp that referenced this pull request Dec 26, 2025
)

* model: llama-embed-nemotron

* minor: python lint

* changed arch-name

* templated llm_build_llama to be used for both llama and llama-embed arch
blime4 referenced this pull request in blime4/llama.cpp Feb 5, 2026
* model: llama-embed-nemotron

* minor: python lint

* changed arch-name

* templated llm_build_llama to be used for both llama and llama-embed arch
Seunghhon pushed a commit to Seunghhon/llama.cpp that referenced this pull request Apr 26, 2026
)

* model: llama-embed-nemotron

* minor: python lint

* changed arch-name

* templated llm_build_llama to be used for both llama and llama-embed arch
my-other-github-account pushed a commit to my-other-github-account/llama.cpp that referenced this pull request May 15, 2026
)

* model: llama-embed-nemotron

* minor: python lint

* changed arch-name

* templated llm_build_llama to be used for both llama and llama-embed arch
fukuro-kun pushed a commit to fukuro-kun/fukuro-llama-cpp-turboquant that referenced this pull request Jul 5, 2026
)

* model: llama-embed-nemotron

* minor: python lint

* changed arch-name

* templated llm_build_llama to be used for both llama and llama-embed arch
MrLordCat referenced this pull request in MrLordCat/llama.cpp-rdna-lab Jul 16, 2026
* model: llama-embed-nemotron

* minor: python lint

* changed arch-name

* templated llm_build_llama to be used for both llama and llama-embed arch
zommiommy pushed a commit to zommiommy/llama.cpp that referenced this pull request Aug 18, 2026
)

* model: llama-embed-nemotron

* minor: python lint

* changed arch-name

* templated llm_build_llama to be used for both llama and llama-embed arch
frostyautumnleaf pushed a commit to frostyautumnleaf/llama.cpp that referenced this pull request Oct 5, 2026
)

* model: llama-embed-nemotron

* minor: python lint

* changed arch-name

* templated llm_build_llama to be used for both llama and llama-embed arch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

model Model specific python python script changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants