6

Mac 运行 chatglm2-6b

 1 year ago
source link: https://blog.kelu.org/tech/2023/06/30/mac-chatglm2-6b.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
mac.jpg

我是在 Mac studio M2 Max上运行的。这篇文章记录运行的过程。以下是我的一些版本信息:

  • MacOS 13.4
  • 共享内存:96G
  • conda 23.5.0
  • Python 3.11.4
  • pip 23.1.2

一、环境准备

如果你还不熟悉python使用,可以参考我之前关于conda相关的文章,切换到虚拟环境进行操作。

在GitHub上下载源码。https://github.com/THUDM/ChatGLM2-6B

使用国内源(清华)安装依赖,否则速度很慢。

pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
image-20230702073622684

二、下载模型

https://cloud.tsinghua.edu.cn/d/674208019e314311ab5c/

也可以去 huggingface.co上下载模型。不过我没有用。

brew install git-lfs
image-20230702074106842
image-20230702074138924
pip install gradio -i https://pypi.tuna.tsinghua.edu.cn/simple

可以在代码里指定下载好的模型地址。我是运行 python web_demo.py后等待开始下载,然后直接替换缓存。

我的默认的下载路径是这个:

~/.cache/huggingface/hub/models--THUDM--chatglm2-6b/snapshots/c57e892806dfe383cd5caf09719628788fe96379
image-20230702093408246

把下载好的文件直接替换这几个bin文件即可:

image-20230702133231166

三、运行demo

1. webdemo

python web_demo.py
image-20230702102520122
image-20230702101903189

可以注意到有warning:

/modeling_chatglm.py:1173: UserWarning: MPS: no support for int64 min/max ops, casting it to int32 (Triggered internally at /Users/runner/work/_temp/anaconda/conda-bld/pytorch_1682343668887/work/aten/src/ATen/native/mps/operations/ReduceOps.mm:1271.)
  if unfinished_sequences.max() == 0 or stopping_criteria(input_ids, scores):

运行demo2:

 pip install streamlit streamlit-chat -i https://pypi.tuna.tsinghua.edu.cn/simple
image-20230702105953743
streamlit run web_demo2.py
image-20230702110501103

2. 命令行demo

image-20230702111002384

3. api

image-20230702111601859
curl -X POST "http://127.0.0.1:8000" \
     -H 'Content-Type: application/json' \
     -d '{"prompt": "你和chatgpt哪个更好?", "history": []}'
image-20230702111734556
image-20230702111758665

四、一些遇到的问题:

  1. 只要开了系统代理就会报这个错。

     requests.exceptions.SSLError: HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /THUDM/chatglm2-6b/resolve/main/tokenizer_config.json (Caused by SSLError(SSLEOFError(8, '[SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol (_ssl.c:1002)')))
    

    查看了很多信息也无法解决。我把代理关掉之后又有:

         assert os.path.isfile(model_path), model_path
                ^^^^^^^^^^^^^^^^^^^^^^^^^^
       File "<frozen genericpath>", line 30, in isfile
     TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType
    

    这个报错是缺少模型文件。

    所以不开代理就不会自动下载文件找不到模型,开了代理就下载不了模型。

    最后的解决办法竟然是开全局代理。如果是开规则模式的话,则要把 huggingface.co 加入进去。

  2. 运行报错:

       File "/Users/kelu/Workspace/Miniforge3/envs/pytorch_env/lib/python3.11/site-packages/torch/cuda/__init__.py", line 239, in _lazy_init
         raise AssertionError("Torch not compiled with CUDA enabled")
     AssertionError: Torch not compiled with CUDA enabled
    

    还是要仔细看 官方文档介绍,Mac部署需要修改模型的载入方法:

     model = AutoModel.from_pretrained("your local path", trust_remote_code=True).to('mps')
    

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK