license: apache-2.0
datasets:
- code-search-net/code_search_net
pipeline_tag: fill-mask
tags:
- code
metrics:
- code_eval
new_version: Shuu12121/CodeHawks-ModernBERT
CodeMorph-ModernBERT
概述
CodeMorph-ModernBERT 是一款专为代码搜索与代码理解任务从头训练的预训练模型。本模型基于 code-search-net/code_search_net
数据集训练,强化了对代码语义的理解能力。
支持最大2048个token的序列长度(传统微软模型仅支持512token),在Python代码搜索任务中表现尤为突出。
- 架构: ModernBERT基础架构
- 用途: 代码搜索/代码理解/代码补全
- 训练数据: CodeSearchNet(全语种)
- 许可证: Apache 2.0
核心特性
-
长序列支持
可处理长达2048个token的序列,适用于冗长代码和复杂函数
-
卓越的代码搜索性能
采用面向6种编程语言的SentencePiece分词器,搜索精度显著超越前代模型
-
专项训练模型
基于CodeSearchNet数据集从头训练,深入理解代码语法与注释关联
参数配置
模型采用以下参数设计:
参数名 |
设定值 |
词汇量 |
50000 |
隐藏层维度 |
768 |
隐藏层数 |
12 |
注意力头数 |
12 |
中间层维度 |
3072 |
最大位置编码 |
2048 |
类型词汇量 |
2 |
隐藏层dropout概率 |
0.1 |
注意力dropout概率 |
0.1 |
局部注意力窗口 |
128 |
旋转位置编码基数 |
160000 |
局部注意力旋转基数 |
10000 |
使用指南
通过Hugging Face Transformers库可快速加载本模型(需4.48.0
及以上版本)
加载模型
from transformers import AutoModelForMaskedLM, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("Shuu12121/CodeMorph-ModernBERT")
model = AutoModelForMaskedLM.from_pretrained("Shuu12121/CodeMorph-ModernBERT")
掩码填充
from transformers import pipeline
fill_mask = pipeline("fill-mask", model=model, tokenizer=tokenizer)
print(fill_mask("def add_numbers(a, b): return a + [MASK]"))
获取代码嵌入
import torch
def get_embedding(text, model, tokenizer, device="cuda"):
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=256)
if "token_type_ids" in inputs:
inputs.pop("token_type_ids")
inputs = {k: v.to(device) for k, v in inputs.items()}
outputs = model.model(**inputs)
embedding = outputs.last_hidden_state[:, 0, :]
return embedding
embedding = get_embedding("def my_function(): pass", model, tokenizer)
print(embedding.shape)
训练数据
本模型使用code-search-net/code_search_net
数据集训练,该数据集包含Python/Java/JavaScript等多语言代码片段,是代码搜索任务的理想选择。
评估表现
基于code_x_glue_ct_code_to_text
数据集的Python子集评估结果如下:
完整实验记录详见此链接
指标 |
得分 |
平均倒数排名(MRR) |
0.8172 |
平均准确率(MAP) |
0.8172 |
R-精确率 |
0.7501 |
召回率@10 |
0.9389 |
准确率@10 |
0.8143 |
NDCG@10 |
0.8445 |
F1值@10 |
0.8423 |
横向对比
与其他主流代码搜索模型的对比结果:
模型 |
MRR |
MAP |
R-精确率 |
CodeMorph-ModernBERT |
0.8172 |
0.8172 |
0.7501 |
微软/graphcodebert-base |
0.5482 |
0.5482 |
0.4458 |
微软/codebert-base-mlm |
0.5243 |
0.5243 |
0.4378 |
Salesforce/codet5p-220m-py |
0.7512 |
0.7512 |
0.6617 |
Salesforce/codet5-large-ntp-py |
0.7846 |
0.7846 |
0.7067 |
Shuu12121/CodeMorph-BERT |
0.6851 |
0.6851 |
0.5934 |
Shuu12121/CodeMorph-BERTv2 |
0.6535 |
0.6535 |
0.5543 |
代码搜索专项评估(google/code_x_glue_tc_nl_code_search_adv测试集)
在候选池规模为100的设置下,各模型表现对比:
补充实验代码参见此仓库
模型 |
MRR |
MAP |
R-精确率 |
CodeMorph-ModernBERT |
0.6107 |
0.6107 |
0.5038 |
Salesforce/codet5p-220m-py |
0.5037 |
0.5037 |
0.3805 |
Salesforce/codet5-large-ntp-py |
0.4872 |
0.4872 |
0.3658 |
微软/graphcodebert-base |
0.3844 |
0.3844 |
0.2764 |
微软/codebert-base-mlm |
0.3766 |
0.3766 |
0.2683 |
CodeMorph-BERTv2 |
0.3142 |
0.3142 |
0.2166 |
CodeMorph-BERT |
0.2978 |
0.2978 |
0.1992 |
多语言评估
基于1000条抽样数据的多语言评估结果:
完整实验参见此笔记本
语言 |
MRR |
MAP |
R-精确率 |
Python |
0.8098 |
0.8098 |
0.7520 |
Java |
0.6437 |
0.6437 |
0.5480 |
JavaScript |
0.5928 |
0.5928 |
0.4880 |
PHP |
0.7512 |
0.7512 |
0.6710 |
Ruby |
0.7188 |
0.7188 |
0.6310 |
Go |
0.5358 |
0.5358 |
0.4320 |
值得注意的是,Salesforce/codet5p-220m-bimodal模型在多数语种上表现更优:
语言 |
MRR |
MAP |
R-精确率 |
Python |
0.8322 |
0.8322 |
0.7660 |
Java |
0.8886 |
0.8886 |
0.8390 |
JavaScript |
0.7611 |
0.7611 |
0.6710 |
PHP |
0.8985 |
0.8985 |
0.8530 |
Ruby |
0.7635 |
0.7635 |
0.6740 |
Go |
0.8127 |
0.8127 |
0.7260 |
但在google/code_x_glue_tc_nl_code_search_adv测试集上,本模型展现优势:
模型 |
MRR |
MAP |
R-精确率 |
CodeMorph-ModernBERT |
0.6107 |
0.6107 |
0.5038 |
Salesforce/codet5p-220m-bimodal |
0.5326 |
0.5326 |
0.4208 |
许可协议
本模型采用Apache-2.0
开源协议。
联系方式
如有疑问请联系:shun0212114@outlook.jp