| --- |
| license: cc-by-nc-sa-4.0 |
| tags: |
| - sentence-transformers |
| - transformers |
| - splade |
| - sparse-encoder |
| - code |
| pipeline_tag: feature-extraction |
| --- |
| |
| SPLADE-Code-8B is a sparse retrieval model designed for code retrieval tasks. |
|
|
| ## Usage |
|
|
| ### Using Sentence Transformers |
|
|
| Install Sentence Transformers: |
| ```bash |
| pip install sentence_transformers |
| ``` |
|
|
| ```python |
| from sentence_transformers import SparseEncoder |
| |
| model = SparseEncoder("naver/splade-code-8B", trust_remote_code=True) |
| |
| queries = [ |
| "SELECT *\nFROM Student\nWHERE Age = (\nSELECT MAX(Age)\nFROM Student\nWHERE Group = 'specific_group'\n)\nAND Group = 'specific_group';" |
| ] |
| |
| query_embeddings = model.encode(queries) |
| print(query_embeddings.shape) |
| # torch.Size([1, 151936]) |
| |
| sparsity = model.sparsity(query_embeddings) |
| print(sparsity) |
| # {'active_dims': 1120.0, 'sparsity_ratio': 0.9926284751474305} |
| |
| decoded = model.decode(query_embeddings, top_k=10) |
| print(decoded) |
| # [[ |
| # ('Δ group', 2.34375), |
| # ('Δ oldest', 2.28125), |
| # ('Δ age', 2.25), |
| # ('_group', 2.25), |
| # ('Δ Group', 2.171875), |
| # ('Δ Age', 2.109375), |
| # ('Δ MAX', 2.0625), |
| # ('Δ Student', 2.046875), |
| # ('Δ specific', 2.03125), |
| # ('Δ student', 2.0), |
| # ]] |
| ``` |
|
|
| ### Using Transformers |
|
|
| ```bash |
| pip install transformers |
| ``` |
|
|
| ```python |
| from transformers import AutoModelForCausalLM, AutoModel |
| import os |
| import torch |
| |
| splade = AutoModelForCausalLM.from_pretrained("naver/splade-code-8B", trust_remote_code=True) |
| device = (torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")) |
| splade.to(device) |
| splade.eval() |
| queries = ["SELECT *\nFROM Student\nWHERE Age = (\nSELECT MAX(Age)\nFROM Student\nWHERE Group = 'specific_group'\n)\nAND Group = 'specific_group';"] |
| bow_dict = splade.encode(queries, prompt_type="query", top_k_q=10, return_dict=True, print_dict=True) |
| ``` |
|
|
| ``` |
| +--------------------------------------------------------------------+ |
| | TOP ACTIVATED WORDS | |
| +--------------------------------------------------------------------+ |
| |
| |
| * INPUT: SELECT * |
| FROM Student |
| WHERE Age = ( |
| SELECT MAX(Age) |
| FROM Student |
| WHERE Group = 'specific_group' |
| ) |
| AND Group = 'specific_group'; |
| |
| Δ group | ββββββββββββββββββββ 2.34 |
| Δ oldest | βββββββββββββββββββ 2.28 |
| Δ age | βββββββββββββββββββ 2.25 |
| _group | βββββββββββββββββββ 2.25 |
| Δ Group | ββββββββββββββββββ 2.17 |
| Δ Age | ββββββββββββββββββ 2.11 |
| Δ MAX | βββββββββββββββββ 2.06 |
| Δ Student | βββββββββββββββββ 2.05 |
| Δ specific | βββββββββββββββββ 2.03 |
| Δ student | βββββββββββββββββ 2.00 |
| ``` |
|
|
|
|