CGE:基于Causal LLM的Code Embedding模型

news2025/3/12 19:05:04

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

以下是 CodeFuse-CGE 项目的相关开源介绍,如果对这部分内容感兴趣,欢迎访问我们的项目主页 GitHub - 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 地址

  • https://huggingface.co/codefuse-ai/CodeFuse-CGE-Small

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 上运行训练。

实验结果

表1:NL2Code Benchmarks

表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 ,支持我们

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2147868.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Qt 窗口事件机制

在 Qt 开发中,窗口的关闭、隐藏、显示等事件是常见且重要的功能。不同的事件触发条件、处理方式不同,了解和掌握这些事件有助于我们更好地控制窗口行为。本文将详细讲解这些事件的使用方法,并通过代码实例来展示其应用。 1. done(int r) — 关…

9.19总结

这几天学习了网络流 1,EK ek的主要思路是不断通过bfs找到增广路,找到增广路再建立反向边,直到不能再bfs到汇点,为什么可以通过建反向边呢?以上图举例,上图走完第一条增广路建立了一条反向边,当…

fps pve制作

1 导入素材 将人物模型和骨骼导入(直接将fps拖进去,选择正确的骨骼即可) 将枪支模型导入,取消创建骨骼,将静态网格体导入其中 2创建角色蓝图,也就是我们玩家控制的对象 然后在角色的组件中找到网格体并使…

几何 | 数学专项

日期内容2024.9.19创建 { d > 0 , 递增数列 d < 0 , 递减数列 d 0 &#xff0c;常数列 \begin{cases} d>0,递增数列\\ d<0,递减数列\\ d0&#xff0c;常数列 \end{cases} ⎩ ⎨ ⎧​d>0,递增数列d<0,递减数列d0&#xff0c;常数列​ 【2010.13】 【1.历年真…

【算法题】46. 全排列-力扣(LeetCode)

【算法题】46. 全排列-力扣(LeetCode) 1.题目 下方是力扣官方题目的地址 46. 全排列 给定一个不含重复数字的数组 nums &#xff0c;返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。 示例 1&#xff1a; 输入&#xff1a;nums [1,2,3] 输出&#xff1a;[[1,2,3…

漏件、丢件常发生?海外仓智能化管理来解决

随着海外仓业务的迅猛发展&#xff0c;一些仓库在日常运营中出现了出入库操作不规范、库存管理不够严格等问题。这些问题多数源于高度依赖人工操作及随意性较强的管理方式&#xff0c;导致了库存数据不准确、货物遗漏或丢失等情况的发生。长此以往&#xff0c;不仅会增加客户的…

让自动驾驶系统无限逼近人类?最新混合规划器实现高度安全的实车导航

导读&#xff1a; 本篇文章针对基于学习的规划器难以保证安全闭环驾驶这一问题&#xff0c;提出了一种新型的混合运动规划器&#xff0c;其结合了基于学习和基于优化的技术。通过仿真实验和实车实验&#xff0c;证明了本文规划器的有效性和鲁棒性。©️【深蓝AI】编译 1. 摘…

【已解决】Uncaught RangeError: Maximum depth reached

【已解决】Uncaught RangeError: Maximum depth reached 在JavaScript编程中&#xff0c;Uncaught RangeError: Maximum depth reached 是一个常见的错误&#xff0c;通常与递归调用深度过大有关。递归是一种编程技巧&#xff0c;它允许函数直接或间接地调用自身。然而&#xf…

2024年轻人驯化AI指南

或许Python编程是答案 我为您精心准备了一份全面的Python学习大礼包&#xff0c;完全免费分享给每一位渴望成长、希望突破自我现状却略感迷茫的朋友。无论您是编程新手还是希望深化技能的开发者&#xff0c;都欢迎加入我们的学习之旅&#xff0c;共同交流进步&#xff01; &…

OpenAI GPT o1技术报告阅读(2)- 关于模型安全性的测试案例

✨报告阅读&#xff1a;使用大模型来学习推理(Reason) 首先是原文链接&#xff1a;https://openai.com/index/learning-to-reason-with-llms/ 接下来我们看一个简单的关于模型安全性的测试&#xff0c;当模型被问到一个有风险的话题时&#xff0c;会如何思考并回答用户呢&…

saltstack入门

一、saltstack入门 一、saltstack介绍 1、saltstack简述 SaltStack 是一种基于 C/S 架构的服务器基础架构集中化管理平台&#xff0c;管理端称为 Master&#xff0c;客户端称为 Minion。SaltStack 具备配置管理、远程执行、监控等功能&#xff0c;一般可以理解为是简化版的 Pup…

安卓Settings值原理源码剖析存储最大的字符数量是多少?

背景&#xff1a; 平常做rom相关开发时候经常需要与settings值打交道&#xff0c;需要独立或者存储一个settings的场景&#xff0c;群里有个学员朋友就问了一个疑问&#xff0c;那就是Settings的putString方式来存储字符&#xff0c;那么可以存储的最大字符是多少呢&#xff1…

初始c++:入门基础(完结)

打字不易&#xff0c;留个赞再走吧~~~ 目录 一函数重载二引用1 引⽤的概念和定义2引⽤的特性3引⽤的使⽤三inline四nullptr 一函数重载 C⽀持在同⼀作⽤域中出现同名函数&#xff0c;但是要求这些同名函数的形参不同&#xff0c;可以是参数个数不同或者 类型不同。这样C函数调⽤…

【数据结构-差分】【hard】力扣995. K 连续位的最小翻转次数

给定一个二进制数组 nums 和一个整数 k 。 k位翻转 就是从 nums 中选择一个长度为 k 的 子数组 &#xff0c;同时把子数组中的每一个 0 都改成 1 &#xff0c;把子数组中的每一个 1 都改成 0 。 返回数组中不存在 0 所需的最小 k位翻转 次数。如果不可能&#xff0c;则返回 -…

代码随想录算法训练营43期 | Day 20 —— 235.二叉搜索树的最近公共祖先、701.二叉搜索树中的插入操作、450.删除二叉搜索树中的节点

代码随想录算法训练营 代码随想录算法训练营43期235.二叉搜索树的最近公共祖先701.二叉搜索树中的插入操作450.删除二叉搜索树中的节点 代码随想录算法训练营43期 235.二叉搜索树的最近公共祖先 解题思路&#xff1a; 二叉搜索树一定是有序的 判断条件&#xff1a; cur>p &…

解决phpstudy无法启动MySQL服务

三种方法 如果说你在小皮里面&#xff0c;启动mysql&#xff0c;发现启动不了&#xff0c;而且你在你自己电脑本地有装过mysql服务&#xff0c;那么可以按照我下面的不走来&#xff0c;按顺序试验1&#xff0c;2&#xff0c;3,三个里面肯定有一个是可以解决的 1.停止本地的mysq…

整整3W字笔记,Redis最核心的秒杀业务、分布式锁、消息队列相关原理一篇文章就搞定(黑马点评项目)

目录 四、 优惠卷秒杀系列功能实现 4.1 全局ID生成器 4.1.1 全局ID生成器的选型 4.1.2 全局ID生成器的实现 4.1.3 全局ID生成器的测试 4.1.4 其他ID生成器的拓展 4.2 利用PostMan模拟管理员后台添加秒杀优惠卷信息 【代码实现】 【PostMan测试】 4.3 优惠卷秒杀下单功能…

Jenkins 构建后操作(Send build artifacts over SSH)

Jenkins 构建后操作(Send build artifacts over SSH) 针对Jenkins部署项目的注意事项 配置Send build artifacts over SSH SSH Server,这是一个系统配置 配置地址&#xff1a;系统管理 -> 系统配置 ->SSH Server 注意1&#xff1a;记得点一下高级里面有一个密码配置&…

10 vue3之全局组件,局部组件,递归组件,动态组件

全局组件 使用频率非常高的组件可以搞成全局组件&#xff0c;无需再组件中再次import引入 在main.ts 注册 import Card from ./components/Card/index.vuecreateApp(App).component(Card,Card).mount(#app) 使用方法 直接在其他vue页面 立即使用即可 无需引入 <templat…

240919-Pip先在线下载不安装+再离线安装

A. 最终效果 # 使用modelscope sdk下载模型 import os os.environ[MODELSCOPE_CACHE] 您希望的下载路径from modelscope import snapshot_download model_dir snapshot_download(opendatalab/PDF-Extract-Kit) print(f"模型文件下载路径为&#xff1a;{model_dir}/model…