• LLM 系列 | 21 : Code Llama实战(上篇) : 模型简介与评测


    引言

    小伙伴们好,我是《小窗幽记机器学习》的小编:卖热干面的小女孩。

    个人CSDN首页:JasonLiu1919_面向对象的程序设计,深度学习,C++-CSDN博客

    今天开始以2篇小作文介绍代码大语言模型Code Llama。上篇主要介绍Code Llama的基本情况并基于HuggingFace上部署的Demo快速体验下Code Llama的实战效果,下篇则主要介绍如何在本地部署Code Llama。感兴趣的小伙伴可以关注下!

    模型简介

    Code Llama是基于Llama 2面向编程领域的大型代码语言模型(即代码大模型),基于该工具可以使用文本提示(Prompt)直接生成或者理解代码。Code Llama具备包括代码补全能力,最长可以生成 100k 个token。此外,Code Llama还具备编程任务的零样本指令遵循能力,即面向自然语言的指令编程。官方宣称Code Llama在公开的编程任务中效果最好,能够使开发人员的工作流程更快速、更高效,并降低编程的学习门槛。Code Llama 在用作生产力和教育工具方面潜力巨大,能够帮助程序员编写更加健壮、有良好文档的软件。

    目前(2023年9月12日)代码语言模型的leaderboard结果如下:

    Code Llama 是 Llama 2 的代码专用版本,在特定代码数据集上进一步训练 Llama 2 并从同一数据集中采样更多数据,进行更长时间训练。相对于Llama 2,Code Llama的编码能力得到提升,可以根据代码和自然语言提示(例如:“编写一个输出斐波那契数列的函数”)生成代码,也可以进行代码解读。Code Llama还可以用于代码补全和调试。Code Llama支持当下流行的多种编程语言,包括Python、C++、Java、PHP、TypeScript(JavaScript)、C#、Bash等。

    此次官方发布了三种参数规模的 Code Llama,分别为 7B、13B 和 34B。这些模型都基于 500B 个tokens的 代码和与代码相关的数据进行训练。7B 和 13B 的Code Llama模型和Code Llama instruct模型还引入with fill-in-the-middle(填充中间,FIM)的训练方式,使其能够将代码插入到现有代码中,这意味着可以直接支持诸如代码填充之类的任务。

    这三种模型适用于不同延迟要求和服务场景。比如,7B 模型可以部署在单个GPU上,34B 模型返回结果最佳,可以提供更好地辅助编码,但更小的 7B 和 13B 模型速度更快,更适用于需要低延迟的任务,如实时代码补全。

    官方还进一步微调了 Code Llama 的两个变体:Code Llama - Python 和 Code Llama - Instruct。

    • 「Code Llama - Python」 是针对特定语言Python的Code Llama,在 100B个token Python 代码上进行进一步微调。Python是代码生成中最常用的benchmarked语言,且Python和PyTorch在AI社区中扮演着重要角色。Code Llama-Python这个专门的模型可以提供额外的效用。

    • 「Code Llama - Instruct」 是一种经过指令微调和对齐的Code Llama变体。指令微调其实是继续训练,只是目标函数不同。模型会接收自然语言指令作为输入,再输出符合要求的输出。Instruct的方式使得模型在理解人类自然语言形式提示的时候表现更好。在代码生成场景可以优先使用 Code Llama - Instruct。

    「Code Llama、Code Llama - Python、Code Llama - Instruct」这三类模型之间的关系如下:

    与 Llama 2 一样,官方对模型的微调版本应用了相当大的安全缓解措施。有关模型训练、架构和参数、评估、AI安全性等详细信息,可以参阅研究论文。Meta官方宣称,对AI采取开放的方式,是发展创新、安全和负责任的新AI工具的最佳方式,因此他们将 Code Llama 以相同的社区许可证(与 Llama 2 相同)发布,供研究和商业用途使用。

    以下通过 huggingface 上发布的Demo实测Code Llama在代码任务上的效果。

    先直接剧透下结论:

    功能任务是否正常运行结果是否符合预期总体评估
    代码补全(13B版)判断是否存在中文字符yesyes⭐⭐⭐⭐⭐
    代码补全(13B版)pandas指定列增加行数据nono⭐⭐
    代码填充(13B版)去除非ascii字符yesyes⭐⭐⭐⭐⭐
    指令编程(13B版)pandas指定列增加行数据nono⭐⭐
    指令编程(33B版)pandas指定列增加行数据yesno⭐⭐⭐

    Code Llama实测(13B版):代码补全

    为了快速亲测效果,使用官方基于CodeLlama-13b-hf发布的Demo。需要注意的是,这里使用的模型不是Chat版,所以只能用于代码补齐,不能对话方式生成代码。

    测试1:判断是否有中文字符

    输入:

    1. define a functionCheck whether Chinese characters exist
    2. def check_cn_char(test_str):

    输出:

    1. # define a function, Check whether Chinese characters exist
    2. def check_cn_char(test_str):
    3.     for c in test_str:
    4.         if u'\u4e00' <= c <= u'\u9fff':
    5.             return True
    6.     return False
    7. # define a function, Check whether English characters exist
    8. def check_en_char(test_str):
    9.     for c in test_str:
    10.         if 'a' <= c <= 'z' or 'A' <= c <= 'Z':
    11.             return True
    12.     return False
    13. # define a function, Check whether numbers exist
    14. def check_num(test_str):
    15.     for c in test_str:
    16.         if '0' <= c <= '9':
    17.             return True
    18.     return False
    19. # define a function, Check whether special characters exist
    20. def check_special_char(test_str):
    21.     for c in test_str:
    22.         if not (u'\u4e00' <= c <= u'\u9fff'and not ('a' <= c <= 'z' or 'A' <= c <= 'Z'and not ('0' <= c <= '9'):
    23.             return True
    24.     return False
    25. # define a function, Check whether the password

    测试check_cn_char函数,能够正常运行,且结果符合预期。

    测试2:pandas指定列增加行数据

    输入:

    1. # define a function, append pandas column with input test_list
    2. def df_append_rows(test_df,target_column, append_list)

    输出结果如下:

    1. # define a function, append pandas column with input test_list
    2. def df_append_rows(test_df,target_column, append_list) :
    3.     for i in range(len(append_list)) :
    4.         test_df[target_column= test_df[target_column].append(append_list[i])
    5.     return test_df
    6. # define a function, append pandas column with input test_list
    7. def df_append_rows_with_index(test_df,target_column, append_list) :
    8.     for i in range(len(append_list)) :
    9.         test_df[target_column= test_df[target_column].append(append_list[i], ignore_index=True)
    10.     return test_df
    11. # define a function, append pandas column with input test_list
    12. def df_append_rows_with_index_and_columns(test_df,target_column, append_list) :
    13.     for i in range(len(append_list)) :
    14.         test_df[target_column= test_df[target_column].append(append_list[i], ignore_index=Truesort=False)
    15.     return test_df
    16. # define a function, append pandas column with input test_

    直接测试df_append_rows函数,报错:

    TypeError: cannot concatenate object of type 'str'>'; only Series and DataFrame objs are valid
    

    Code Llama实测(13B版):代码填充

    由于代码填充功能仅局限于Code Llama和 Code Llama - Instruct的7B 和 13B 版模型中,本文选用Code Llama-13B进行实测,Demo地址进行体验。

    输入:

    1. def remove_non_ascii(s: str-> str:
    2.     ""
    3.     return result

    输出结果:

    1. def remove_non_ascii(s: str) -> str:
    2.     """ Remove non-ASCII characters from a string.
    3.     Args:
    4.         s (str): The string to be processed.
    5.     Returns:
    6.         str: The processed string.
    7.     """
    8.     result = ""
    9.     for c in s:
    10.         if ord(c) < 128:
    11.             result += c
    12.     return result

    Code Llama实测(13B版):指令编程

    使用官方基于CodeLlama-13b-Instruct-hf发布的Demo进行体验。

    输入:

    I have a pandas DataFrame df['text'], how can I directly add a list of data test_list to df['text'] to increase the number of rows?
    

    输出:

    上述代码测试,报错:

    TypeError: cannot concatenate object of type 'list'>'; only Series and DataFrame objs are valid
    

    可以看出,Code Llama对于pandas部分函数的理解有待提高。

    Code Llama实测(34B版):指令编程

    在 huggingface 上选择codellama/CodeLlama-34b-Instruct-hf

    输出结果:

    生成的代码能够运行,但是结果不符合预期:

    1. import pandas as pd
    2. # create a sample dataframe with one column 'text'
    3. df = pd.DataFrame({'text': ['hello''world']})
    4. # create a list of new text values to be appended
    5. new_values = ['goodbye''cruel world']
    6. # append the new values to the 'text' column
    7. df['text'].append(pd.Series(new_values))
    8. print(df)

    需要人工修正下:

    1. # 以下是人工修改的结果,才可以得到符合预期的结果
    2. tmp_df = df['text'].append(pd.Series(new_values))  # 人工修改
    3. print(tmp_df)

    总结

    本文简要介绍Code Llama模型概况,同时通过huggingface上的Demo快速实测Code Llama的效果。撇开官方提供的case之后,亲测的几个case,虽然存在一些瑕疵,但整体效果也算还不错,期待后续的优化。

    目前很多程序员已经在各种任务中使用LLM来协助开发。这确实使开发者的工作更高效,以便开发者可以专注于最具人本特色的方面,而不是重复性的任务。AI模型,特别是用于编码用途的LLM,从开放式的方法中受益最大。无论是在创新还是在安全性方面,公开可用的代码专用模型可以促进新技术的发展,从而改善人们的生活。未来可期!

  • 相关阅读:
    【产业前沿】树莓集团如何以数字媒体产业园为引擎,加速产业升级?
    华为云服务器实例测评
    FusionInsight MRS Flink DataStream API读写Hudi实践
    《操作系统-真象还原》09. 线程
    使用FastApi服务解决程序反复调试导致速度过慢的问题(以tsfresh为例)
    TS的类型编程
    回放线上流量利器-GoReplay
    mybatis的分页&&查询返回结果集的处理
    【二进制部署k8s-1.29.4】六、kube-scheduler安装部署
    mysql安装
  • 原文地址:https://blog.csdn.net/ljp1919/article/details/133843105