Views
No views yet
wget https://raw.githubusercontent.com/microsoft/CodeBERT/master/UniXcoder/unixcoder.py1import torch
2from unixcoder import UniXcoder
3
4device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
5model = UniXcoder("microsoft/unixcoder-base")
6model.to(device)1# Encode maximum function
2func = "def f(a,b): if a>b: return a else return b"
3tokens_ids = model.tokenize([func],max_length=512,mode="<encoder-only>")
4source_ids = torch.tensor(tokens_ids).to(device)
5tokens_embeddings,max_func_embedding = model(source_ids)
6
7# Encode minimum function
8func = "def f(a,b): if a<b: return a else return b"
9tokens_ids = model.tokenize([func],max_length=512,mode="<encoder-only>")
10source_ids = torch.tensor(tokens_ids).to(device)
11tokens_embeddings,min_func_embedding = model(source_ids)
12
13# Encode NL
14nl = "return maximum value"
15tokens_ids = model.tokenize([nl],max_length=512,mode="<encoder-only>")
16source_ids = torch.tensor(tokens_ids).to(device)
17tokens_embeddings,nl_embedding = model(source_ids)
18
19print(max_func_embedding.shape)
20print(max_func_embedding)1torch.Size([1, 768])
2tensor([[ 8.6533e-01, -1.9796e+00, -8.6849e-01, 4.2652e-01, -5.3696e-01,
3 -1.5521e-01, 5.3770e-01, 3.4199e-01, 3.6305e-01, -3.9391e-01,
4 -1.1816e+00, 2.6010e+00, -7.7133e-01, 1.8441e+00, 2.3645e+00,
5 ...,
6 -2.9188e+00, 1.2555e+00, -1.9953e+00, -1.9795e+00, 1.7279e+00,
7 6.4590e-01, -5.2769e-02, 2.4965e-01, 2.3962e-02, 5.9996e-02,
8 2.5659e+00, 3.6533e+00, 2.0301e+00]], device='cuda:0',
9 grad_fn=<DivBackward0>)< and >), UniXcoder can distinguish them.1# Normalize embedding
2norm_max_func_embedding = torch.nn.functional.normalize(max_func_embedding, p=2, dim=1)
3norm_min_func_embedding = torch.nn.functional.normalize(min_func_embedding, p=2, dim=1)
4norm_nl_embedding = torch.nn.functional.normalize(nl_embedding, p=2, dim=1)
5
6max_func_nl_similarity = torch.einsum("ac,bc->ab",norm_max_func_embedding,norm_nl_embedding)
7min_func_nl_similarity = torch.einsum("ac,bc->ab",norm_min_func_embedding,norm_nl_embedding)
8
9print(max_func_nl_similarity)
10print(min_func_nl_similarity)1tensor([[0.3002]], device='cuda:0', grad_fn=<ViewBackward>)
2tensor([[0.1881]], device='cuda:0', grad_fn=<ViewBackward>)1context = """
2def f(data,file_path):
3 # write json data into file_path in python language
4"""
5tokens_ids = model.tokenize([context],max_length=512,mode="<decoder-only>")
6source_ids = torch.tensor(tokens_ids).to(device)
7prediction_ids = model.generate(source_ids, decoder_only=True, beam_size=3, max_length=128)
8predictions = model.decode(prediction_ids)
9print(context+predictions[0][0])1def f(data,file_path):
2 # write json data into file_path in python language
3 data = json.dumps(data)
4 with open(file_path, 'w') as f:
5 f.write(data)1context = """
2def <mask0>(data,file_path):
3 data = json.dumps(data)
4 with open(file_path, 'w') as f:
5 f.write(data)
6"""
7tokens_ids = model.tokenize([context],max_length=512,mode="<encoder-decoder>")
8source_ids = torch.tensor(tokens_ids).to(device)
9prediction_ids = model.generate(source_ids, decoder_only=False, beam_size=3, max_length=128)
10predictions = model.decode(prediction_ids)
11print([x.replace("<mask0>","").strip() for x in predictions[0]])['write_json', 'write_file', 'to_json']1context = """
2def write_json(data,file_path):
3 data = <mask0>(data)
4 with open(file_path, 'w') as f:
5 f.write(data)
6"""
7tokens_ids = model.tokenize([context],max_length=512,mode="<encoder-decoder>")
8source_ids = torch.tensor(tokens_ids).to(device)
9prediction_ids = model.generate(source_ids, decoder_only=False, beam_size=3, max_length=128)
10predictions = model.decode(prediction_ids)
11print([x.replace("<mask0>","").strip() for x in predictions[0]])['json.dumps', 'json.loads', 'str']1context = """
2# <mask0>
3def write_json(data,file_path):
4 data = json.dumps(data)
5 with open(file_path, 'w') as f:
6 f.write(data)
7"""
8tokens_ids = model.tokenize([context],max_length=512,mode="<encoder-decoder>")
9source_ids = torch.tensor(tokens_ids).to(device)
10prediction_ids = model.generate(source_ids, decoder_only=False, beam_size=3, max_length=128)
11predictions = model.decode(prediction_ids)
12print([x.replace("<mask0>","").strip() for x in predictions[0]])['Write JSON to file', 'Write json to file', 'Write a json file']@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}
}