File size: 6,344 Bytes
6340e30
 
 
 
 
 
 
 
 
6c0272b
 
6340e30
 
 
b0c0d8f
4b8390f
b0c0d8f
4b8390f
b0c0d8f
 
 
 
 
 
 
 
 
4b8390f
 
b0c0d8f
 
 
4b8390f
b0c0d8f
e656c91
b0c0d8f
4b8390f
b0c0d8f
 
 
 
 
 
 
4b8390f
 
 
 
 
 
b0c0d8f
4b8390f
 
b0c0d8f
4b8390f
 
 
 
b0c0d8f
 
e656c91
 
 
 
 
 
 
 
b0c0d8f
e656c91
 
b0c0d8f
 
 
 
 
4b8390f
b0c0d8f
e656c91
 
 
 
b0c0d8f
 
e656c91
 
 
b0c0d8f
e656c91
 
b0c0d8f
 
4b8390f
 
b0c0d8f
4b8390f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0c0d8f
4b8390f
b0c0d8f
 
 
 
 
 
 
 
 
4b8390f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
---
library_name: sentence-transformers
pipeline_tag: sentence-similarity
tags:
- swe-bench
- code-similarity
- code-retrieval
- code-search
- code-explanation
- sentence-transformers
- sentence-similarity
widget: []
---

## Introduction

The NV-EmbedCode model is a 7B Mistral-based embedding model optimized for code retrieval, supporting text, code, and hybrid queries.

Code retrieval is a critical task in many domains including coding assistance, code explanation, summarization, and documentation search. NV-EmbedCode transforms the input code or textual data into dense vector representations, known as embeddings, enabling effective retrieval and search.

For technical details, refer to our paper: [Nemotron-CORTEXA: Enhancing LLM Agents for Software Engineering Tasks via Improved Localization and Solution Diversity](https://openreview.net/forum?id=k6p8UKRdH7)

## Intended use
The NV-EmbedCode model is most suitable for users who want to build a code retrieval system over a large text or code corpus, leveraging the latest dense retrieval technologies. <br>
 
### License/Terms of Use
The use of this model is governed by the [NVIDIA AI Foundation Models Community License Agreement](https://developer.nvidia.com/downloads/nv-ai-foundation-models-license) and the [Apache License 2.0](https://choosealicense.com/licenses/apache-2.0/).

## Model Details
- **Base model:** [NVIDIA Retrieval QA Mistral 7B Embedding model](https://build.nvidia.com/nvidia/nv-embedqa-mistral-7b-v2/modelcard)
- **Embedding dimension:** 4096
- **Pooling mode:** mean tokens

## Evaluation Results:
We evaluated NV-EmbedCode model using the [CoIR benchmark](https://arxiv.org/html/2407.02883v1) and a curated set based on [SWE-bench](https://arxiv.org/abs/2310.06770). CoIR consists of 10 code datasets across four retrieval tasks: (1) Text-to-Code Retrieval, (2) Code-to-Code Retrieval, (3) Code-to-Text Retrieval, and (4) Hybrid Code Retrieval. The default evaluation metric for CoIR is average NDCG@10 across all datasets. SWE-bench originally consists of real-world software engineering problems from GitHub issues and their corresponding pull requests. We adapted it into a retrieval task, where the goal is to identify the files that need to be edited to resolve an issue. These files are identified using the pull request that solved the issue. For SWE-bench Lite, we use Recall@1 to measure whether the top retrieved file is the correct one for resolving the issue, as each instance typically involves editing just one file. For more detailed evaluation results on SWE-bench, please refer to [our paper](https://openreview.net/forum?id=k6p8UKRdH7).
<br>

| Retrieval Method | CoIR Main Score (NDCG@10) | SWE-bench Lite (Recall@1) |
|:------------|:---------------:|-------------:|
| NV-EmbedCode | 72.45% | 70.33% |
| NV-EmbedQA-Mistral-7B-v2 | 60.08% | 61.33% |
| SFR-Embedding-Code-2B_R | 67.41% | 47.00% |
| SFR-Mistral-2_R | 61.85% | 60.33% |
| BM25 | - | 42.33% |


## Usage

### Direct Usage (Sentence Transformers)

First install the following libraries:

```bash
pip install transformers==4.37.2 sentence_transformers
```

Then you can load this model and run inference.
```python
from sentence_transformers import SentenceTransformer, util

# Task instructions for different retrieval scenarios
task_instructions = {
    "general": "Retrieve code or text based on user query",
    "originalbug": "Given a bug description, retrieve codes that need to be edited to resolve it.",
    "llmsummary": "Given a summary of bug description generated by an LLM, retrieve codes that need to be edited to resolve it."
}

# Example queries and corpus
queries = [
    "Function to calculate the sum of two numbers",
    "Recursive function to calculate the factorial of a number",
]

docs = [
    "def add(a, b):\n    return a + b",
    "def factorial(n):\n    return 1 if n==0 else n*factorial(n-1)",
]

# Prepare prompt prefix for corpus
query_prefix = f"Instruct: {task_instructions['general']}\nQuery: "

# Load model
model = SentenceTransformer('nvidia/NV-EmbedCode-7b-v1', trust_remote_code=True)

# Encode queries and documents
query_emb = model.encode(queries, prompt=query_prefix, normalize_embeddings=True)
doc_emb = model.encode(docs, normalize_embeddings=True)

# Compute similarity scores
scores = util.cos_sim(query_emb, doc_emb) * 100
print(scores.tolist())
# [[68.55826568603516, 24.0609130859375], [28.60508918762207, 76.94281005859375]]
```


<!--
### Direct Usage (Transformers)

<details><summary>Click to see the direct usage in Transformers</summary>

</details>
-->

<!--
### Downstream Usage (Sentence Transformers)

You can finetune this model on your own dataset.

<details><summary>Click to expand</summary>

</details>
-->

<!--
### Out-of-Scope Use

*List how the model may foreseeably be misused and address what users ought not to do with the model.*
-->

<!--
## Bias, Risks and Limitations

*What are the known or foreseeable issues stemming from this model? You could also flag here known failure cases or weaknesses of the model.*
-->

<!--
### Recommendations

*What are recommendations with respect to the foreseeable issues? For example, filtering explicit content.*
-->

### Framework Versions
- Python: 3.11.10
- Sentence Transformers: 3.0.0
- Transformers: 4.37.2
- PyTorch: 2.2.0+cu121
- Accelerate: 0.34.2
- Datasets: 3.0.1
- Tokenizers: 0.15.2

## Citation
If you find this model useful in your research, please consider citing:

```
@inproceedings{nemotroncortexa,
  title={Nemotron-{CORTEXA}: Enhancing {LLM} Agents for Software Engineering Tasks via Improved Localization and Solution Diversity},
  author={Atefeh Sohrabizadeh and Jialin Song and Mingjie Liu and Rajarshi Roy and Chankyu Lee and Jonathan Raiman and Bryan Catanzaro},
  booktitle={Forty-second International Conference on Machine Learning},
  year={2025},
  url={https://openreview.net/forum?id=k6p8UKRdH7}
}
```

<!--
## Glossary

*Clearly define terms in order to be accessible across audiences.*
-->

<!--
## Model Card Authors

*Lists the people who create the model card, providing recognition and accountability for the detailed work that goes into its construction.*
-->

<!--
## Model Card Contact

*Provides a way for people who have updates to the Model Card, suggestions, or questions, to contact the Model Card authors.*
-->