🚀 🦉CodeModernBERT-Owl
CodeModernBERT-Owl 是一個專門為代碼搜索和代碼理解任務從頭設計的預訓練模型。與之前的版本如 CodeHawks-ModernBERT 和 CodeMorph-ModernBERT 相比,該模型現在支持 Rust 語言,並提高了在 Python、PHP、Java、JavaScript、Go 和 Ruby 等語言中的搜索準確性。
🚀 快速開始
這個模型可以使用 Hugging Face Transformers 庫輕鬆加載。
⚠️ 重要提示
需要 transformers >= 4.48.0
🔗 Colab 演示(替換為 "CodeModernBERT-Owl")
模型的加載
from transformers import AutoModelForMaskedLM, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("Shuu12121/CodeModernBERT-Owl")
model = AutoModelForMaskedLM.from_pretrained("Shuu12121/CodeModernBERT-Owl")
獲取代碼嵌入
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)
✨ 主要特性
- ✅ 支持長達 2048 個標記的長序列(相比微軟 512 個標記的模型)
- ✅ 針對代碼搜索、代碼理解和代碼克隆檢測進行了優化
- ✅ 在 GitHub 開源倉庫(Java、Rust)上進行了微調
- ✅ 在 CodeHawks/CodeMorph 系列中實現了最高的準確性
- ✅ 多語言支持:Python、PHP、Java、JavaScript、Go、Ruby 和 Rust
📚 詳細文檔
模型參數
屬性 |
詳情 |
模型類型 |
預訓練模型 |
訓練數據 |
Shuu12121/rust-codesearch-dataset-open、Shuu12121/java-codesearch-dataset-open、code-search-net/code_search_net、google/code_x_glue_ct_code_to_text |
vocab_size |
50,004 |
hidden_size |
768 |
num_hidden_layers |
12 |
num_attention_heads |
12 |
intermediate_size |
3,072 |
max_position_embeddings |
2,048 |
type_vocab_size |
2 |
hidden_dropout_prob |
0.1 |
attention_probs_dropout_prob |
0.1 |
local_attention_window |
128 |
rope_theta |
160,000 |
local_attention_rope_theta |
10,000 |
評估結果
數據集
- 📌 在
code_x_glue_ct_code_to_text
上進行測試,候選池大小為 100。
- 📌 使用
Shuu12121/rust-codesearch-dataset-open
進行特定於 Rust 的評估。
主要評估指標的比較(同一種子值)
語言 |
CodeModernBERT-Owl |
CodeHawks-ModernBERT |
Salesforce CodeT5+ |
Microsoft CodeBERT |
GraphCodeBERT |
Python |
0.8793 |
0.8551 |
0.8266 |
0.5243 |
0.5493 |
Java |
0.8880 |
0.7971 |
0.8867 |
0.3134 |
0.5879 |
JavaScript |
0.8423 |
0.7634 |
0.7628 |
0.2694 |
0.5051 |
PHP |
0.9129 |
0.8578 |
0.9027 |
0.2642 |
0.6225 |
Ruby |
0.8038 |
0.7469 |
0.7568 |
0.3318 |
0.5876 |
Go |
0.9386 |
0.9043 |
0.8117 |
0.3262 |
0.4243 |
- ✅ 在所有目標語言中實現了最高的準確性。
- ✅ 使用額外微調的 GitHub 數據顯著提高了 Java 的準確性。
- ✅ 優於之前的模型,特別是在 PHP 和 Go 語言中。
Rust (獨自數據集)
指標 |
CodeModernBERT-Owl |
MRR |
0.7940 |
MAP |
0.7940 |
R - Precision |
0.7173 |
K 別評估指標
K |
Recall@K |
Precision@K |
NDCG@K |
F1@K |
Success Rate@K |
Query Coverage@K |
1 |
0.7173 |
0.7173 |
0.7173 |
0.7173 |
0.7173 |
0.7173 |
5 |
0.8913 |
0.7852 |
0.8118 |
0.8132 |
0.8913 |
0.8913 |
10 |
0.9333 |
0.7908 |
0.8254 |
0.8230 |
0.9333 |
0.9333 |
50 |
0.9887 |
0.7938 |
0.8383 |
0.8288 |
0.9887 |
0.9887 |
100 |
1.0000 |
0.7940 |
0.8401 |
0.8291 |
1.0000 |
1.0000 |
其他推薦模型
1. CodeSearch-ModernBERT-Owl🦉 (https://huggingface.co/Shuu12121/CodeSearch-ModernBERT-Owl)
如果您需要一個更專門用於代碼搜索的模型,強烈推薦此模型。
2. CodeModernBERT-Snake🐍 (https://huggingface.co/Shuu12121/CodeModernBERT-Snake)
如果您需要一個支持更長序列或更小模型大小的預訓練模型,這個模型是理想的選擇。
- 最大序列長度:8192 個標記
- 更小的模型大小:約 75M 參數
3. CodeSearch-ModernBERT-Snake🐍 (https://huggingface.co/Shuu12121/CodeSearch-ModernBERT-Snake)
對於那些想要一個結合了長序列長度和代碼搜索專業性的模型的人來說,這個模型是最佳選擇。
📝 結論
- ✅ 在所有語言中表現出色
- ✅ 通過數據集擴充成功添加了對 Rust 的支持
- ✅ 使用更好的數據集可能會進一步提高性能
📄 許可證
📄 Apache - 2.0
📧 聯繫方式
📩 如有任何問題,請聯繫:
📧 shun0212114@outlook.jp