microsoft / unixcoder-base

huggingface.co
Total runs: 93.6K
24-hour runs: 0
7-day runs: 1.7K
30-day runs: 1.7K
Model's Last Updated: July 31 2024
feature-extraction

Introduction of unixcoder-base

Model Details of unixcoder-base

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 on huggingface.co

93.6K
Total runs
0
24-hour runs
11.0K
3-day runs
1.7K
7-day runs
1.7K
30-day runs

More Information About unixcoder-base huggingface.co Model

More unixcoder-base license Visit here:

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

unixcoder-base huggingface.co

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

unixcoder-base huggingface.co Url

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

microsoft unixcoder-base online free

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

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

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

unixcoder-base install

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

unixcoder-base install url in huggingface.co:

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

Url of unixcoder-base

unixcoder-base huggingface.co Url

Provider of unixcoder-base huggingface.co

microsoft
ORGANIZATIONS

Other API from microsoft

huggingface.co

Total runs: 681.1K
Run Growth: 208.0K
Growth Rate: 30.53%
Updated:February 03 2022
huggingface.co

Total runs: 595.8K
Run Growth: -131.1K
Growth Rate: -22.00%
Updated:November 25 2025
huggingface.co

Total runs: 535.6K
Run Growth: 307.0K
Growth Rate: 57.32%
Updated:April 08 2024
huggingface.co

Total runs: 511.8K
Run Growth: -531.5K
Growth Rate: -103.84%
Updated:December 08 2025
huggingface.co

Total runs: 474.1K
Run Growth: -1.7K
Growth Rate: -0.37%
Updated:September 26 2022
huggingface.co

Total runs: 289.1K
Run Growth: -22.3K
Growth Rate: -7.70%
Updated:February 14 2024
huggingface.co

Total runs: 117.8K
Run Growth: -147.4K
Growth Rate: -125.12%
Updated:November 08 2023
huggingface.co

Total runs: 117.6K
Run Growth: -1.2K
Growth Rate: -1.03%
Updated:February 29 2024
huggingface.co

Total runs: 100.0K
Run Growth: -425
Growth Rate: -0.42%
Updated:August 28 2025
huggingface.co

Total runs: 95.1K
Run Growth: -22.7K
Growth Rate: -23.87%
Updated:February 03 2023
huggingface.co

Total runs: 69.7K
Run Growth: -13.1K
Growth Rate: -18.74%
Updated:November 25 2025
huggingface.co

Total runs: 68.7K
Run Growth: -18.2K
Growth Rate: -26.47%
Updated:December 03 2025
huggingface.co

Total runs: 52.0K
Run Growth: 26.0K
Growth Rate: 50.00%
Updated:December 23 2021
huggingface.co

Total runs: 32.5K
Run Growth: 28.2K
Growth Rate: 86.85%
Updated:April 23 2026
huggingface.co

Total runs: 29.0K
Run Growth: -90.7K
Growth Rate: -312.44%
Updated:May 12 2026
huggingface.co

Total runs: 28.0K
Run Growth: -48.2K
Growth Rate: -167.74%
Updated:August 04 2021