CGE:基于Causal LLM的Code Embedding模型

本文涉及的产品
模型在线服务 PAI-EAS,A10/V100等 500元 1个月
交互式建模 PAI-DSW,每月250计算时 3个月
模型训练 PAI-DLC,100CU*H 3个月
简介: CodeFuse-CGE 项目在外滩大会展出,吸引众多技术与产品从业者的关注。“文搜代码”功能备受好评,模型表现令人期待。CodeFuse-CGE 采用大语言模型,通过 LoRA 微调提取文本与代码嵌入,实现在多个 NL2Code 基准测试中超越现有 SOTA 模型。现已开源 CGE-Large 与 CGE-Small 两种模型,欢迎访问 GitHub 页并支持本项目。[项目地址](https://github.com/codefuse-ai/CodeFuse-CGE)

近日,CodeFuse-CGE 项目在外滩大会展出,吸引了众多技术、产品从业者的到访,部分参观者表示“文搜代码”令人耳目一新,期待模型后续的表现。

以下是 CodeFuse-CGE 项目的相关开源介绍,如果对这部分内容感兴趣,欢迎访问我们的项目主页https://github.com/codefuse-ai/CodeFuse-CGE

为我们点赞,支持我们的项目。


01简介


Code Embedding 是一种将代码片段转化为向量表示的技术。这种表示形式使得机器学习模型能够更好地理解和处理代码,在自动化程序分析、代码搜索、代码补全,以及自动化测试等领域都起到非常重要的作用。大语言模型(Large Language Models)因为其在大量的语言数据上预训练,可以获得对语义细微表示的能力。最近,LLMs 在代码生成、代码补全等任务上都有非常出色的表现。


目前 Code Embedding 模型主要基于 Encoder 架构,如 CodeBert、Unixcoder 等。又或者基于 Encoder-Decoder 架构,如 CodeT5、CodeT5+ 等。然而局限于架构设计和模型大小,他们很难获取到更丰富的语义表示能力。


我们以 CodeQwen1.5-7B-Chat 和 Phi-3.5-mini-instruct 模型作为基座模型,通过一个交叉注意力计算模块来提取输入序列的 Embedding,将文本表征和代码表征投射到同一空间中。我们的方法可以激发出基座模型强大的代码、文本的语义表示能力。实验表明我们的方法在 CSN 和 AdvTest 这 2 个 NL2Code Benchmarks 上都有着超越 SOTA 的能力。我们将开源 CGE-Large 和 CGE-Small 两种大小的模型。


TLDR

CGE 即 Code General Embedding。我们提出了一种基于大语言模型的获取 Embedding 方案,通过 Lora 微调来借助大语言模型的语义能力,激发其语义表征能力,在 2 个 NL2Code Benchmarks 上达到了 SOTA 的表现。


🏡Homepage:

https://github.com/codefuse-ai/CodeFuse-CGE  

hCGE

(Please give us your support with a Star🌟 + Fork🚀 + Watch 👀)



02 开源模型


CodeFuse-CGE-Large

huggingface 地址

https://huggingface.co/codefuse-ai/CodeFuse-CGE-Large


Model Configuration

  • Base Model:CodeQwen1.5-7B-Chat
  • Model Size:7B
  • Embedding Dimension:1024


Requirements

flash_attn==2.4.2
torch==2.1.0
accelerate==0.28.0
transformers==4.39.2 
vllm=0.5.3


CodeFuse-CGE-Small

huggingface 地址


Model Configuration

  • Base Model:Phi-3.5-mini-instruct
  • Model Size:3.8B
  • Embedding Dimension:1024


Requirements

flash_attn==2.4.2
torch==2.1.0
accelerate==0.28.0
transformers>=4.43.0


How to Use?

Transformers

import torch
from transformers import AutoTokenizer, AutoModel
model_name_or_path = "codefuse-ai/CodeFuse-CGE-Large"
model = AutoModel.from_pretrained(model_name_or_path, trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, trust_remote_code=True, truncation_side='right', padding_side='right')
if torch.cuda.is_available():
    device = 'cuda'
else:
    device = 'cpu'
model.to(device)
prefix_dict =  {'python':{'query':'Retrieve the Python code that solves the following query:', 'passage':'Python code:'},
                'java':{'query':'Retrieve the Java code that solves the following query:', 'passage':'Java code:'},
                'go':{'query':'Retrieve the Go code that solves the following query:', 'passage':'Go code:'},
                'c++':{'query':'Retrieve the C++ code that solves the following query:', 'passage':'C++ code:'},
                'javascript':{'query':'Retrieve the Javascript code that solves the following query:', 'passage':'Javascript code:'},
                'php':{'query':'Retrieve the PHP code that solves the following query:', 'passage':'PHP code:'},
                'ruby':{'query':'Retrieve the Ruby code that solves the following query:', 'passage':'Ruby code:'},
                'default':{'query':'Retrieve the code that solves the following query:', 'passage':'Code:'}
                }
text = ["Writes a Boolean to the stream.",
        "def writeBoolean(self, n): t = TYPE_BOOL_TRUE if n is False: t = TYPE_BOOL_FALSE self.stream.write(t)"]
text[0] += prefix_dict['python']['query']
text[1] += prefix_dict['python']['passage']
embed = model.encode(tokenizer, text)
score = embed[0] @ embed[1].T
print("score", score)


Vllm

我们同时适配了 vllm 的部署,来减少部署时的时延。可参考我们的 github 仓库https://github.com/codefuse-ai/CodeFuse-CGE

from vllm import ModelRegistry
from utils.vllm_codefuse_cge_large import CodeFuse_CGE_Large
from vllm.model_executor.models import ModelRegistry
from vllm import LLM
def always_true_is_embedding_model(model_arch: str) -> bool:
    return True
ModelRegistry.is_embedding_model = always_true_is_embedding_model
ModelRegistry.register_model("CodeFuse_CGE_Large", CodeFuse_CGE_Large)
model_name_or_path = "codefuse-ai/CodeFuse-CGE-Large"
model = LLM(model=model_name_or_path, trust_remote_code=True, enforce_eager=True, enable_chunked_prefill=False)
prefix_dict =  {'python':{'query':'Retrieve the Python code that solves the following query:', 'passage':'Python code:'},
                'java':{'query':'Retrieve the Java code that solves the following query:', 'passage':'Java code:'},
                'go':{'query':'Retrieve the Go code that solves the following query:', 'passage':'Go code:'},
                'c++':{'query':'Retrieve the C++ code that solves the following query:', 'passage':'C++ code:'},
                'javascript':{'query':'Retrieve the Javascript code that solves the following query:', 'passage':'Javascript code:'},
                'php':{'query':'Retrieve the PHP code that solves the following query:', 'passage':'PHP code:'},
                'ruby':{'query':'Retrieve the Ruby code that solves the following query:', 'passage':'Ruby code:'},
                'default':{'query':'Retrieve the code that solves the following query:', 'passage':'Code:'}
                }
text = ["Return the best fit based on rsquared",
        "def find_best_rsquared ( list_of_fits ) : res = sorted ( list_of_fits , key = lambda x : x . rsquared ) return res [ - 1 ]"]
text[0] += prefix_dict['python']['query']
text[1] += prefix_dict['python']['passage']
embed_0 = model.encode([text[0]])[0].outputs.embedding
embed_1 = model.encode([text[1]])[0].outputs.embedding
注:1、在适配 Vllm 之后,模型的输入只能设置为批量大小为 1;否则会导致数组溢出错误。
2、目前仅对 CodeFuse-CGE-Large 模型进行了适配,CodeFuse-CGE-Small 模型的支持将在不久后提供。



03 实验


我们以 CodeQwen1.5-7B-Chat 作为模型底座,主要在 Text2Code Retrieval 这个任务上去验证算法的有效性。用于评测的数据集包括:AdvTest、CSN 以及 CosQA 数据集。评测指标我们主要使用的 MRR。我们在 32 张 80 G A100 上运行训练。


实验结果



image.png


表1:NL2Code Benchmarks


image.png

图1:NL2Code Benchmarks雷达图


从实验结果上看,CodeFuse-CGE-Large 的结果优于目前的SOTA,AdvTest 超过 CodeSAGE-Large 2.61%,在 CosQA 数据集上超过 CodeSAGE-Large 6.37% ,而在 CSN 数据集上,在 php 语言和 go 语言上和 CodeT5P-110M-embedding 表现出相当的水平,在其他语言上都超过 CodeT5P-110M-embedding 2%-4%,在 CSN 的平均水平上超过 CodeT5P-110M-embedding 1.8% 的水平。相比于目前在 Code Embedding 领域场景的两种架构, Encoder 以及 Encoder-Decoder 模型,我们模型的结果都有显著优越的结果,而对目前 OpenAI 商用的 Embedding API,我们的结果也在各个方面都超过了商用 Embedding API 的水平。


同时,我们也训练了输出更小维度的 Embedding 模型,从表 1 中可以看到,输出 384 维的模型在 AdvTest 和 CosQA 数据集上相比输出 1024 维的模型效果相当,只是在 CSN 数据集上有 3% 的下降。在实际使用中,如果考虑到存储的问题,可以使用输出 384 维的 Embedding 模型来减少存储压力,同时不会降低太多的性能。


CodeFuse-CGE-Small 的结果相较于 CodeFuse-CGE-Large 在 AdvTest 和 CSN 数据集上分别有着 9% 和 6% 的效果下降,在 CosQA 上有着 2% 的提升。如果考虑到实际使用的时延以及部署机器的限制,可以考虑使用 CodeFuse-CGE-Small 模型。


04 总结


我们基于 Code LLM 作为底座模型,通过 PMA 提取输入 text 或者 token 的 Embedding,在经过 LoRA 微调后,得到了一个对齐 text 和 code 之间语义的 Embedding 模型。实验结果表明,在 Text2Code Retrieval 测试集上都超越了目前 SOTA 的结果。


参考文献

[1] Zhu, Yutao, et al. "Large language models for information retrieval: A survey." arxiv preprint arxiv:2308.07107 (2023).

[2] Zhang, Dejiao, et al. "Code representation learning at scale." arXiv preprint arXiv:2402.01935 (2024).

[3] Feng, Zhangyin, et al. "Codebert: A pre-trained model for programming and natural languages." arXiv preprint arXiv:2002.08155 (2020).

[4] Wang, Yue, et al. "Codet5+: Open code large language models for code understanding and generation." arXiv preprint arXiv:2305.07922 (2023).

[5] Guo, Daya, et al. "Graphcodebert: Pre-training code representations with data flow." arXiv preprint arXiv:2009.08366 (2020).

[6] Zhou, Haoyi, et al. "Informer: Beyond efficient transformer for long sequence time-series forecasting." Proceedings of the AAAI conference on artificial intelligence. Vol. 35. No. 12. 2021.

[7] Guo, Daya, et al. "Unixcoder: Unified cross-modal pre-training for code representation." arXiv preprint arXiv:2203.03850 (2022).


CodeFuse 相关模型和数据集陆续开源中,如果您喜欢我们的工作,欢迎试用、指正错误和贡献代码,也可以给我们的项目增加 Star ,支持我们


技术交流与探讨,你还可以加入我们的用户交流群


目录
相关文章
|
5月前
|
人工智能 自然语言处理 测试技术
能够双向推理的LLM!Dream-7B:港大联合华为开源的扩散推理模型,能够同时考虑前后文信息
Dream-7B是由香港大学与华为诺亚方舟实验室联合研发的开源扩散大语言模型,采用独特的掩码扩散范式,在文本生成、数学推理和代码编写等任务中展现出卓越性能。
245 3
能够双向推理的LLM!Dream-7B:港大联合华为开源的扩散推理模型,能够同时考虑前后文信息
|
3月前
|
机器学习/深度学习 人工智能 PyTorch
200行python代码实现从Bigram模型到LLM
本文从零基础出发,逐步实现了一个类似GPT的Transformer模型。首先通过Bigram模型生成诗词,接着加入Positional Encoding实现位置信息编码,再引入Single Head Self-Attention机制计算token间的关系,并扩展到Multi-Head Self-Attention以增强表现力。随后添加FeedForward、Block结构、残差连接(Residual Connection)、投影(Projection)、层归一化(Layer Normalization)及Dropout等组件,最终调整超参数完成一个6层、6头、384维度的“0.0155B”模型
195 11
200行python代码实现从Bigram模型到LLM
|
4月前
|
存储 JSON PyTorch
Multimodal LLM训练-模型文件\训练数据加载逻辑源码分析
Multimodal LLM训练-模型文件\训练数据加载逻辑源码分析
217 17
|
7月前
|
机器学习/深度学习 人工智能 监控
X-R1:3090也能训7B模型!开源框架X-R1把训练成本打下来了:10美元训出企业级LLM
X-R1 是一个基于强化学习的低成本训练框架,能够加速大规模语言模型的后训练开发。仅需4块3090或4090 GPU,1小时内完成训练,成本低于10美元。
352 5
X-R1:3090也能训7B模型!开源框架X-R1把训练成本打下来了:10美元训出企业级LLM
|
7月前
|
机器学习/深度学习 人工智能 测试技术
仅7B的模型数学推理能力完虐70B?MIT哈佛推出行动思维链COAT让LLM实现自我反思并探索新策略
Satori 是由 MIT 和哈佛大学等机构联合推出的 7B 参数大型语言模型,专注于提升推理能力,具备强大的自回归搜索和自我纠错功能。
245 6
仅7B的模型数学推理能力完虐70B?MIT哈佛推出行动思维链COAT让LLM实现自我反思并探索新策略
|
7月前
|
存储 Kubernetes 测试技术
企业级LLM推理部署新范式:基于ACK的DeepSeek蒸馏模型生产环境落地指南
企业级LLM推理部署新范式:基于ACK的DeepSeek蒸馏模型生产环境落地指南
289 12
|
8月前
|
自然语言处理 算法 JavaScript
面向长文本的多模型协作摘要架构:多LLM文本摘要方法
多LLM摘要框架通过生成和评估两个步骤处理长文档,支持集中式和分散式两种策略。每个LLM独立生成文本摘要,集中式方法由单一LLM评估并选择最佳摘要,而分散式方法则由多个LLM共同评估,达成共识。论文提出两阶段流程:先分块摘要,再汇总生成最终摘要。实验结果显示,多LLM框架显著优于单LLM基准,性能提升最高达3倍,且仅需少量LLM和一轮生成评估即可获得显著效果。
270 10
面向长文本的多模型协作摘要架构:多LLM文本摘要方法
|
7月前
|
存储 Kubernetes 测试技术
企业级LLM推理部署新范式:基于ACK的DeepSeek蒸馏模型生产环境落地指南
本教程演示如何在ACK中使用vLLM框架快速部署DeepSeek R1模型推理服务。
|
7月前
|
数据采集 人工智能 监控
Crawl4LLM:你的模型还在吃垃圾数据?CMU博士开源AI爬虫,自动筛选高价值网页,数据抓取质量飙升300%
Crawl4LLM 是清华大学和卡内基梅隆大学联合开发的智能爬虫系统,通过网页价值评估和优先级队列技术,显著提升大语言模型预训练数据采集效率。
372 4
|
7月前
|
机器学习/深度学习 搜索推荐 异构计算
LLM模型添加自定义Token代码示例:为Llama 3.2模型添加思考与回答标记
本文将介绍如何为大型语言模型(LLM)添加自定义token并进行训练,使模型能够有效地利用这些新增token。以Llama 3.2模型为基础,实现了类似DeepSeek R1中think和answer标记功能的扩展方法,通过监督微调使模型学习使用这些标记进行推理过程与答案输出的区分
401 0
LLM模型添加自定义Token代码示例:为Llama 3.2模型添加思考与回答标记

热门文章

最新文章