microsoft / unixcoder-base-nine

huggingface.co
Total runs: 11.3K
24-hour runs: 0
7-day runs: -651
30-day runs: 3.4K
Model's Last Updated: 7월 31 2024
feature-extraction

Introduction of unixcoder-base-nine

Model Details of unixcoder-base-nine

Model Card for UniXcoder-base

Model Details

Model Description

UniXcoder is a unified cross-modal pre-trained model that leverages multimodal data (i.e. code comment and AST) to pretrain code representation.

  • Developed by: Microsoft Team
  • Shared by [Optional]: Hugging Face
  • Model type: Feature Engineering
  • Language(s) (NLP): en
  • License: Apache-2.0
  • Related Models:
    • Parent Model: RoBERTa
  • Resources for more information:

Uses

1. Dependency
  • pip install torch
  • pip install transformers
2. Quick Tour

We implement a class to use UniXcoder and you can follow the code to build UniXcoder. You can download the class by

wget https://raw.githubusercontent.com/microsoft/CodeBERT/master/UniXcoder/unixcoder.py
import torch
from unixcoder import UniXcoder

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = UniXcoder("microsoft/unixcoder-base")
model.to(device)

In the following, we will give zero-shot examples for several tasks under different mode, including code search (encoder-only) , code completion (decoder-only) , function name prediction (encoder-decoder) , API recommendation (encoder-decoder) , code summarization (encoder-decoder) .

3. Encoder-only Mode

For encoder-only mode, we give an example of code search .

1) Code and NL Embeddings

Here, we give an example to obtain code fragment embedding from CodeBERT.

# Encode maximum function
func = "def f(a,b): if a>b: return a else return b"
tokens_ids = model.tokenize([func],max_length=512,mode="<encoder-only>")
source_ids = torch.tensor(tokens_ids).to(device)
tokens_embeddings,max_func_embedding = model(source_ids)

# Encode minimum function
func = "def f(a,b): if a<b: return a else return b"
tokens_ids = model.tokenize([func],max_length=512,mode="<encoder-only>")
source_ids = torch.tensor(tokens_ids).to(device)
tokens_embeddings,min_func_embedding = model(source_ids)

# Encode NL
nl = "return maximum value"
tokens_ids = model.tokenize([nl],max_length=512,mode="<encoder-only>")
source_ids = torch.tensor(tokens_ids).to(device)
tokens_embeddings,nl_embedding = model(source_ids)

print(max_func_embedding.shape)
print(max_func_embedding)
torch.Size([1, 768])
tensor([[ 8.6533e-01, -1.9796e+00, -8.6849e-01,  4.2652e-01, -5.3696e-01,
         -1.5521e-01,  5.3770e-01,  3.4199e-01,  3.6305e-01, -3.9391e-01,
         -1.1816e+00,  2.6010e+00, -7.7133e-01,  1.8441e+00,  2.3645e+00,
                 ...,
         -2.9188e+00,  1.2555e+00, -1.9953e+00, -1.9795e+00,  1.7279e+00,
          6.4590e-01, -5.2769e-02,  2.4965e-01,  2.3962e-02,  5.9996e-02,
          2.5659e+00,  3.6533e+00,  2.0301e+00]], device='cuda:0',
       grad_fn=<DivBackward0>)
2) Similarity between code and NL

Now, we calculate cosine similarity between NL and two functions. Although the difference of two functions is only a operator ( < and > ), UniXcoder can distinguish them.

# Normalize embedding
norm_max_func_embedding = torch.nn.functional.normalize(max_func_embedding, p=2, dim=1)
norm_min_func_embedding = torch.nn.functional.normalize(min_func_embedding, p=2, dim=1)
norm_nl_embedding = torch.nn.functional.normalize(nl_embedding, p=2, dim=1)

max_func_nl_similarity = torch.einsum("ac,bc->ab",norm_max_func_embedding,norm_nl_embedding)
min_func_nl_similarity = torch.einsum("ac,bc->ab",norm_min_func_embedding,norm_nl_embedding)

print(max_func_nl_similarity)
print(min_func_nl_similarity)
tensor([[0.3002]], device='cuda:0', grad_fn=<ViewBackward>)
tensor([[0.1881]], device='cuda:0', grad_fn=<ViewBackward>)
3. Decoder-only Mode

For decoder-only mode, we give an example of code completion .

context = """
def f(data,file_path):
    # write json data into file_path in python language
"""
tokens_ids = model.tokenize([context],max_length=512,mode="<decoder-only>")
source_ids = torch.tensor(tokens_ids).to(device)
prediction_ids = model.generate(source_ids, decoder_only=True, beam_size=3, max_length=128)
predictions = model.decode(prediction_ids)
print(context+predictions[0][0])
def f(data,file_path):
    # write json data into file_path in python language
    data = json.dumps(data)
    with open(file_path, 'w') as f:
        f.write(data)
4. Encoder-Decoder Mode

For encoder-decoder mode, we give two examples including: function name prediction , API recommendation , code summarization .

1) Function Name Prediction
context = """
def <mask0>(data,file_path):
    data = json.dumps(data)
    with open(file_path, 'w') as f:
        f.write(data)
"""
tokens_ids = model.tokenize([context],max_length=512,mode="<encoder-decoder>")
source_ids = torch.tensor(tokens_ids).to(device)
prediction_ids = model.generate(source_ids, decoder_only=False, beam_size=3, max_length=128)
predictions = model.decode(prediction_ids)
print([x.replace("<mask0>","").strip() for x in predictions[0]])
['write_json', 'write_file', 'to_json']
2) API Recommendation
context = """
def write_json(data,file_path):
    data = <mask0>(data)
    with open(file_path, 'w') as f:
        f.write(data)
"""
tokens_ids = model.tokenize([context],max_length=512,mode="<encoder-decoder>")
source_ids = torch.tensor(tokens_ids).to(device)
prediction_ids = model.generate(source_ids, decoder_only=False, beam_size=3, max_length=128)
predictions = model.decode(prediction_ids)
print([x.replace("<mask0>","").strip() for x in predictions[0]])
['json.dumps', 'json.loads', 'str']
3) Code Summarization
context = """
# <mask0>
def write_json(data,file_path):
    data = json.dumps(data)
    with open(file_path, 'w') as f:
        f.write(data)
"""
tokens_ids = model.tokenize([context],max_length=512,mode="<encoder-decoder>")
source_ids = torch.tensor(tokens_ids).to(device)
prediction_ids = model.generate(source_ids, decoder_only=False, beam_size=3, max_length=128)
predictions = model.decode(prediction_ids)
print([x.replace("<mask0>","").strip() for x in predictions[0]])
['Write JSON to file', 'Write json to file', 'Write a json file']

Reference

If you use this code or UniXcoder, please consider citing us.

@article{guo2022unixcoder,
  title={UniXcoder: Unified Cross-Modal Pre-training for Code Representation},
  author={Guo, Daya and Lu, Shuai and Duan, Nan and Wang, Yanlin and Zhou, Ming and Yin, Jian},
  journal={arXiv preprint arXiv:2203.03850},
  year={2022}
}

Runs of microsoft unixcoder-base-nine on huggingface.co

11.3K
Total runs
0
24-hour runs
151
3-day runs
-651
7-day runs
3.4K
30-day runs

More Information About unixcoder-base-nine huggingface.co Model

More unixcoder-base-nine license Visit here:

https://choosealicense.com/licenses/apache-2.0

unixcoder-base-nine huggingface.co

unixcoder-base-nine huggingface.co is an AI model on huggingface.co that provides unixcoder-base-nine's model effect (), which can be used instantly with this microsoft unixcoder-base-nine model. huggingface.co supports a free trial of the unixcoder-base-nine model, and also provides paid use of the unixcoder-base-nine. Support call unixcoder-base-nine model through api, including Node.js, Python, http.

unixcoder-base-nine huggingface.co Url

https://huggingface.co/microsoft/unixcoder-base-nine

microsoft unixcoder-base-nine online free

unixcoder-base-nine huggingface.co is an online trial and call api platform, which integrates unixcoder-base-nine's modeling effects, including api services, and provides a free online trial of unixcoder-base-nine, you can try unixcoder-base-nine online for free by clicking the link below.

microsoft unixcoder-base-nine online free url in huggingface.co:

https://huggingface.co/microsoft/unixcoder-base-nine

unixcoder-base-nine install

unixcoder-base-nine is an open source model from GitHub that offers a free installation service, and any user can find unixcoder-base-nine on GitHub to install. At the same time, huggingface.co provides the effect of unixcoder-base-nine install, users can directly use unixcoder-base-nine installed effect in huggingface.co for debugging and trial. It also supports api for free installation.

unixcoder-base-nine install url in huggingface.co:

https://huggingface.co/microsoft/unixcoder-base-nine

Url of unixcoder-base-nine

unixcoder-base-nine huggingface.co Url

Provider of unixcoder-base-nine huggingface.co

microsoft
ORGANIZATIONS

Other API from microsoft

huggingface.co

Total runs: 19.4M
Run Growth: 12.4M
Growth Rate: 64.13%
Updated: 2월 14 2024
huggingface.co

Total runs: 2.1M
Run Growth: 281.5K
Growth Rate: 13.71%
Updated: 4월 24 2023
huggingface.co

Total runs: 859.1K
Run Growth: 683.3K
Growth Rate: 79.75%
Updated: 2월 03 2022
huggingface.co

Total runs: 528.5K
Run Growth: 133.9K
Growth Rate: 25.34%
Updated: 2월 28 2023
huggingface.co

Total runs: 230.0K
Run Growth: -46.8K
Growth Rate: -20.36%
Updated: 4월 30 2024
huggingface.co

Total runs: 209.9K
Run Growth: -51.4K
Growth Rate: -24.49%
Updated: 8월 19 2024
huggingface.co

Total runs: 131.5K
Run Growth: -9.1K
Growth Rate: -6.90%
Updated: 4월 30 2024
huggingface.co

Total runs: 107.7K
Run Growth: 52.9K
Growth Rate: 49.08%
Updated: 4월 08 2024
huggingface.co

Total runs: 75.5K
Run Growth: 41.4K
Growth Rate: 54.85%
Updated: 9월 18 2023
huggingface.co

Total runs: 49.0K
Run Growth: -23.7K
Growth Rate: -48.48%
Updated: 2월 03 2023
huggingface.co

Total runs: 27.4K
Run Growth: 13.3K
Growth Rate: 48.68%
Updated: 2월 29 2024
huggingface.co

Total runs: 16.8K
Run Growth: 198
Growth Rate: 1.18%
Updated: 11월 23 2023
huggingface.co

Total runs: 11.6K
Run Growth: 1.1K
Growth Rate: 9.72%
Updated: 12월 23 2021
huggingface.co

Total runs: 10.6K
Run Growth: 6.6K
Growth Rate: 62.51%
Updated: 6월 27 2023
huggingface.co

Total runs: 10.1K
Run Growth: 5.0K
Growth Rate: 49.77%
Updated: 7월 02 2022
huggingface.co

Total runs: 9.5K
Run Growth: 468
Growth Rate: 4.92%
Updated: 4월 30 2024
huggingface.co

Total runs: 9.3K
Run Growth: 2.8K
Growth Rate: 30.09%
Updated: 11월 23 2023
huggingface.co

Total runs: 6.6K
Run Growth: 3.4K
Growth Rate: 51.46%
Updated: 8월 28 2024