本地CPU运行ChatGLM-6B与测试过程记载

[复制链接]
查看1014 | 回复0 | 2023-8-23 12:05:43 | 显示全部楼层 |阅读模式
1. 引入

2023年3月份对NLP注定是一个不平常的月份。


  • 3月14日,OpenAI发布GPT-4
  • 3月15日,清华大学唐杰发布了ChatGLM-6B
  • 3月16日,百度发布文心一言
这些模子都是首发。ChatGLM的参数数目是62亿,训练集是1T标识符的中英双语语料。相比而言,GPT3的参数目级是1750亿,GPT4是100万亿(网传)。ChatGLM-6B作为该领域的低本钱模子,值得一试。
2. 具体配置、运行步骤


  • 下载代码
  1. git clone https://github.com/THUDM/ChatGLM-6B
  2. cd ChatGLM-6B
复制代码

  • 安装依赖
  1. pip install -r requirements.txt
复制代码
可以从requirements中看到,模子是基于pytorch的。
这会不会是阐明NLP更多在用pytorch?该学点torch了。

  • 下载模子
这里有点小坑,它给了两个下载链接,其中一个(https://cloud.tsinghua.edu.cn/d/fb9f16d6dc8f482596c2/)只包括模子部门,还缺少相关的模子配置文件,单用这个没发运行。
推荐从HuggingFace下载模子(参考2),留意全部.bin文件,全部.py文件,全部.json等文件,都必要下载并放到某个目录(放在任何一个地方都可以,假设为 path_model )。

  • 修改代码
我们先用repo中给的测试代码来测试,必要将参考3处的代码,改为如下两行:
  1. tokenizer = AutoTokenizer.from_pretrained(path_model, trust_remote_code=True)
  2. model = AutoModel.from_pretrained(path_model, trust_remote_code=True).float()
复制代码
这里的设置有两个目的:(1)配置模子所在的目录,这里假设为path_model (2)配置为CPU运行(将默认的GPU配置.half().cuda()改为CPU配置.float())。

  • 运行测试
  1. python cli_demo.py
复制代码
本文实验情况为python3.10。
3. 运行结果

cli_demo运行后,可以在命令行接口进行简朴的人机交互。结果如下

  • 熟悉英文,能写代码


  • 能进行简朴的中文推理(虽然结果不对)和中文对话



  • 运行时斲丧大量的资源


  • 内存使用了40.6G
  • CPU是30个逻辑核一起跑的

4. 自己写代码调用模子来运行

用如下几行代码,就能启动模子运行,并输出结果。对于必要换行的题目,也没法用cli_demo,只能用下面这种用法。
  1. from transformers import AutoTokenizer, AutoModel
  2. tokenizer = AutoTokenizer.from_pretrained("../chatglm-6b", trust_remote_code=True)
  3. model = AutoModel.from_pretrained("../chatglm-6b", trust_remote_code=True).float()
  4. model = model.eval()
  5. ques = '''
  6. 下面这段代码的功能是什么?
  7. public String t(final Context context, final String str) {
  8.     return K("getLine1Number", str).a("getLine1Number", new com.aaa.sensitive_api_impl<String>() { // from class: com.aaa.sensitive_impl
  9.         public String e() {
  10.             if (o.l(66666, this)) {
  11.                 return o.w();
  12.             }
  13.             try {
  14.                 com.aaa.sensitive_api_impl.c(GalerieService.APPB, r2, str);
  15.                 TelephonyManager a2 = a.a(context);
  16.                 if (a2 != null) {
  17.                     return a2.getLine1Number();
  18.                 }
  19.                 return "";
  20.             } catch (Exception e) {
  21.                 Logger.e("PPP.PhoneData", e);
  22.                 return "";
  23.             }
  24.         }
  25.         }
  26.     }, "");
  27. }
  28. '''
  29. response, history = model.chat(tokenizer, ques, history=[])
  30. print(response)
复制代码
模子的输出为:
  1. 这段代码提供了一个名为`t()`的函数,它接受一个`Context`对象和一个字符串参数`str`。函数返回一个字符串,该字符串包含一个`getLine1Number()`方法的调用,该方法返回当前用户所在设备的电话号码。
  2. 具体来说,该函数通过以下步骤实现这个功能:
  3. 1. 从类`com.aaa.sensitive_impl`中获取一个名为`e()`的方法。
  4. 2. 将该方法重写为使用`K�$()`运算符调用`getLine1Number()`方法。
  5. 3.意外险用API的实现,该实现使用`a()`方法调用`getLine1Number()`方法,并将返回的结果返回给调用者 involvement(这里是函数`t()`的参数)。
复制代码
模子对这些从APK逆向过来的代码,是有一点理解本事的。
5. 总结

本文给出了 ChatGLM-6B 在本地的配置使用步骤,cli的用法,自己写代码调用的方法与测试结果。
盼望能有更多低本钱的大模子能被开源出来!
参考:


  • https://github.com/THUDM/ChatGLM-6B
  • https://huggingface.co/THUDM/chatglm-6b/tree/main
  • https://github.com/THUDM/ChatGLM-6B/blob/main/cli_demo.py#L5

来源:https://blog.csdn.net/ybdesire/article/details/129701328
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则