vscode 选择 python 的虚拟环境

快捷键cmd+p,输入:

> interp

选择python解释器就可以了。

image-20230702105659367

image-20230702105550779

另外,如何确认本机有多少个 虚拟环境:

conda env list
conda activate myenv
conda list
conda install package_name(包名):安装包

conda create -n pandas python=3.11

Mac 运行 chatglm2-6b

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

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

231009更新:

我把本地的模型全部删除了,更新了一版代码,用默认的THUDM/chatglm2-6b,在cli和web2下是OK的,web前端页面输出不全,不知道什么情况。

image-20231009155735974

比较好奇,它在哪里load的模型?我本地已经全部删除了全部模型,并且我也断网了。🤷

然后发现它的模型又由8个变成7个了,然后mps显卡又能用了。

image-20231009172403777

测试过好几次,只要对话稍微多一点,内存直接要撑爆了。

image-20231009163527028

真的有点伤心。

230818更新:

发现模型由7个变成了8个,老环境无法启动了,重新下载了新的模型,也把代码更新了。主要参考 Github 上这个文档里关于“Mac部署”和“本地加载”模型两个内容,修改使用mps即可:

model = AutoModel.from_pretrained("/Users/kelu/Documents/huggingface/chatglm-6b", trust_remote_code=True).half().to('mps')

但我没有办法正常对话:

image-20230818145248697

使用web_demo2.py 也是同样的问题:

streamlit run web_demo2.py

image-20230818145529072

迫不得已换成了cpu的版本,cpu能跑。

model = AutoModel.from_pretrained("/Users/kelu/Documents/huggingface/chatglm-6b", trust_remote_code=True).float()

担心pytorch版本问题可以用这串命令简单打印:

import torch
import transformers

print(f"PyTorch version: {torch.__version__}")
print(f"transformers version: {transformers.__version__}")

# Check PyTorch has access to MPS (Metal Performance Shader, Apple's GPU architecture)
print(f"Is MPS (Metal Performance Shader) built? {torch.backends.mps.is_built()}")
print(f"Is MPS available? {torch.backends.mps.is_available()}")

# Set the device      
device = "mps" if torch.backends.mps.is_available() else "cpu"
print(f"Using device: {device}")


# Create data and send it to the device
x = torch.rand(size=(3, 4)).to(device)
x

我的版本是 2.0.1。。当然也试过 nightly的版本:

image-20230818161032118

使用 gpu 都是和上边一样的报错。cpu能跑就不管它了。

一、环境准备

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

conda create -n chatglm2_env python=3.11
conda activate chatglm2_env

image-20231009110714802

退出环境:

conda deactivate

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

git clone https://github.com/THUDM/ChatGLM2-6B
cd 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

也可以修改代码中的代码,诸如:

tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True).cuda()

THUDM/chatglm2-6b的替换,例如我的替换为:

"/Users/kelu/Documents/huggingface/chatglm-6b"

三、运行demo

vscode 选择解释器

cmd + p
> interpreter

image-20231009150158738

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')
    

使用新版本的 jekyll,加快编译速度

我的 blog 一直使用 jekyll 3.8.6 来 build blog,这么多年下来攒了上千篇文章,编译速度一直令人发指,最近更是达到了惊人的 2000s。

最近在网上闲逛了 jekyll 编译速度的这个问题,似乎已经解决了,使用了最新的版本编译,速度缩短到了 20s,令我震惊。

image-20230620午後10458304

docker-compose.yml 注意修改的内容为 command 的内容即可:

bundle exec jekyll serve

image-20230620午後10803051


虚拟机 Parallels 安装备忘

本篇文章里我先卸载了老版本 Parallels,再安装特定版本 Parallels。参考 alsyundawy/Parallels

  1. 卸载老版本。

    删除 app 应用后,删除相关配置文件:

    rm -rf ~/Library/Parallels
    rm -rf /Library/Preferences/Parallels
    

    Screenshot 2

    Screenshot

  2. 下载

    https://download.parallels.com/desktop/v18/18.1.1-53328/ParallelsDesktop-18.1.1-53328.dmg
    

    我做了一个备份,不过 1M 小水管的速度会很慢。

  3. 安装

    要求升级,直接关闭窗口即可,会询问是否直接安装

    Screenshot 3

    直接关闭 x,然后选择使用当前版本安装。

    Screenshot 1

  4. 一些其他配置

    退出账号并关闭 Parallels Desktop。参考文末的参考资料下载。我也做了个备份

    pkill -9 prl_disp_service
    
    sudo cp -f prl_disp_service "/Applications/Parallels Desktop.app/Contents/MacOS/Parallels Service.app/Contents/MacOS/prl_disp_service"
    sudo chown root:wheel "/Applications/Parallels Desktop.app/Contents/MacOS/Parallels Service.app/Contents/MacOS/prl_disp_service"
    sudo chmod 755 "/Applications/Parallels Desktop.app/Contents/MacOS/Parallels Service.app/Contents/MacOS/prl_disp_service"
    
    sudo cp -f licenses.json "/Library/Preferences/Parallels/licenses.json"
    sudo chown root:wheel "/Library/Preferences/Parallels/licenses.json"
    sudo chmod 444 "/Library/Preferences/Parallels/licenses.json"
    sudo chflags uchg "/Library/Preferences/Parallels/licenses.json"
    sudo chflags schg "/Library/Preferences/Parallels/licenses.json"
    
    sudo codesign -f -s - --timestamp=none --all-architectures --entitlements ParallelsService.entitlements "/Applications/Parallels Desktop.app/Contents/MacOS/Parallels Service.app/Contents/MacOS/prl_disp_service"
    
  5. 修改host

  6. 问题:

    1. 无法联网

      重新安装 Parallels Tools。

      image-20230616午後35749215

      Screenshot 6

      重启系统,就好了。

    2. 修改系统 host 的命令备忘。

      # 加锁
      sudo chflags schg /etc/hosts
      sudo chflags uchg /etc/hosts
             
      # 解锁
      sudo chflags nouchg /etc/hosts
      sudo chflags noschg /etc/hosts
      

参考资料


ipad 下的终端工具 iSH

iOS 上我一直在用 prompt 作为 ssh 登录的工具。但直到我最近使用 Neovim 时候,才意识到它的问题——页面会定时的出现一个 q 字符。非常影响使用体验。

上网搜索了一番,发现了这个不错的命令行工具:https://github.com/ish-app/ish 官网:https://ish.app

根据官方的介绍,iSH是一个x86模拟器,在iOS设备上本地运行Linux shell环境。

image-20230614午後43018693

这篇文章简单记录它安装 ssh 工具的经过,作为一个终端,竟然没有自带 ssh 工具,这是我始料未及的。

  1. 下载。

    https://apps.apple.com/us/app/ish-shell/id1436902243

  2. 先查一下操作系统信息:

    cat /etc/issue
    cat /etc/os-release
    

    IMG_5D6D89A97ABC-1

    可以看到运行的是 alpine 系统,alpine 是一个非常精简的 linux 系统。

  3. 在 alpine 上安装 openssh

    alpine 的源管理工具是 apk,可以使用 apk 进行软件安装:

    apk update
    apk search openssh
    apk add openssh
    

    IMG_2B0E88FFB193-1

  4. 完成。

    非常完美。

    IMG_6534

  5. 字体安装

    可以参考这篇文章操作:neovim 安装使用备忘