Views
No views yet
| Size | CoSQA | AdvTest | CSN-Py | CSN-Ja | CSN-JS | CSN-PHP | CSN-Go | CSN-Ruby | Avg | |
|---|---|---|---|---|---|---|---|---|---|---|
| Openai-Embedding-Ada-002 | Unknown | 0.4423 | 0.3808 | 0.6802 | 0.7149 | 0.6750 | 0.6062 | 0.8563 | 0.7472 | 0.6378 |
| jina-embeddings-v2-base-code | 161M | 0.6837 | 0.385 | 0.6634 | 0.6803 | 0.6304 | 0.5701 | 0.8595 | 0.7095 | 0.6477 |
| CodeSage-large | 1.3B | 0.4753 | 0.5267 | 0.7077 | 0.7021 | 0.695 | 0.6133 | 0.8371 | 0.7192 | 0.6595 |
| CodeFuse-CGE-Small | 3.8B | 0.5619 | 0.4639 | 0.6958 | 0.6863 | 0.6564 | 0.6133 | 0.8637 | 0.7341 | 0.6594 |
| OASIS-1.3B | 1.3B | 0.5532 | 0.4861 | 0.7110 | 0.7199 | 0.6727 | 0.6217 | 0.8732 | 0.7333 | 0.6713 |
1pip install -U torch
2pip install -U transformers1import torch
2import torch.nn.functional as F
3
4from torch import Tensor
5from transformers import AutoModel, AutoTokenizer
6
7def last_token_pool(last_hidden_states: Tensor, attention_mask: Tensor) -> Tensor:
8 left_padding = (attention_mask[:, -1].sum() == attention_mask.shape[0])
9 if left_padding:
10 return last_hidden_states[:, -1]
11 else:
12 sequence_lengths = attention_mask.sum(dim=1) - 1
13 batch_size = last_hidden_states.shape[0]
14 return last_hidden_states[torch.arange(batch_size, device=last_hidden_states.device), sequence_lengths]
15
16# Add query prompt
17def get_query_prompt(query: str):
18 query_description = 'Given a code search query, retrieve relevant code snippet that answer the query'
19 prompt = f'Instruct: {query_description}\nQuery: {query}'
20 return prompt
21
22query = "How to do quicksort in python?"
23
24code1 = """def bubble_sort(arr):
25 n = len(arr)
26 for i in range(n):
27 swapped = False
28 for j in range(1, n - i):
29 if arr[j - 1] > arr[j]:
30 arr[j - 1], arr[j] = arr[j], arr[j - 1]
31 swapped = True
32 if not swapped:
33 break
34 return arr"""
35
36code2 = """def quick_sort(arr):
37 if len(arr) <= 1:
38 return arr
39 else:
40 pivot = arr[0]
41 less = [x for x in arr[1:] if x <= pivot]
42 greater = [x for x in arr[1:] if x > pivot]
43 return quick_sort(less) + [pivot] + quick_sort(greater)"""
44
45model = AutoModel.from_pretrained("Kwaipilot/OASIS-code-1.3B", output_hidden_states=True)
46tokenizer = AutoTokenizer.from_pretrained("Kwaipilot/OASIS-code-1.3B")
47
48# Tokenize and inference
49inputs = tokenizer([get_query_prompt(query), code1, code2], max_length=8192, padding=True, truncation=True, return_tensors='pt')
50outputs = model(**inputs)
51
52# Last token pooling
53embeddings = last_token_pool(outputs.hidden_states[-1], inputs['attention_mask'])
54print(embeddings.shape)
55# torch.Size([3, 2048])
56
57embeddings = F.normalize(embeddings, dim=1, p=2)
58similarity = embeddings @ embeddings.T
59print(similarity[0, 1:])
60# tensor([0.6495, 0.8036])pip install -U sentence-transformers1from sentence_transformers import SentenceTransformer
2
3# Download from the 🤗 Hub
4model = SentenceTransformer("Kwaipilot/OASIS-code-1.3B")#, model_kwargs={"torch_dtype": torch.bfloat16})
5
6query = "How to do quicksort in python?"
7
8code1 = """def bubble_sort(arr):
9 n = len(arr)
10 for i in range(n):
11 swapped = False
12 for j in range(1, n - i):
13 if arr[j - 1] > arr[j]:
14 arr[j - 1], arr[j] = arr[j], arr[j - 1]
15 swapped = True
16 if not swapped:
17 break
18 return arr"""
19
20code2 = """def quick_sort(arr):
21 if len(arr) <= 1:
22 return arr
23 else:
24 pivot = arr[0]
25 less = [x for x in arr[1:] if x <= pivot]
26 greater = [x for x in arr[1:] if x > pivot]
27 return quick_sort(less) + [pivot] + quick_sort(greater)"""
28
29# Run inference
30query_embedding = model.encode([query], prompt_name="query")
31code_embeddings = model.encode([code1, code2])
32
33print(code_embeddings.shape)
34# (2, 2048)
35
36# Get the similarity scores for the embeddings
37print(model.similarity(query_embedding[0], code_embeddings[0]))
38print(model.similarity(query_embedding[0], code_embeddings[1]))
39# tensor([[0.6495]])
40# tensor([[0.8036]])1@misc{kwaipilotoasis,
2 title = {Optimized Augmentation Strategy for Improved code Search},
3 author = {Kwaipilot team},
4 year = {2024},
5}