ChatGLM-6B icon indicating copy to clipboard operation
ChatGLM-6B copied to clipboard

RuntimeError: Unknown platform: darwin

Open LeeeSe opened this issue 3 years ago • 45 comments

Mac support?

LeeeSe avatar Mar 14 '23 07:03 LeeeSe

Seems built with cuda kernels, thus no Mac

qizhanchan avatar Mar 14 '23 08:03 qizhanchan

MacBook, got a same Error.

TengLi931128 avatar Mar 14 '23 08:03 TengLi931128

+1

oltah avatar Mar 14 '23 08:03 oltah

Currently, quantization requires the use of custom CUDA kernels. Without quantization, inference should be able to support Macs with FP16 precision. I will fix this issue later.

Sengxian avatar Mar 14 '23 08:03 Sengxian

+1

wangsouc avatar Mar 14 '23 15:03 wangsouc

Please notifty us if it's fixed. Thanks!

SirLYC avatar Mar 15 '23 02:03 SirLYC

You can actually hack your way in and run the demo on macOS M1 machine. You only need to change 2 lines:

https://github.com/THUDM/ChatGLM-6B/blob/999dd3cb6129c995390642a1f253c1ae2886f286/web_demo.py#L5

Change .half().cuda() to .float()

https://huggingface.co/THUDM/chatglm-6b/blob/9d1509a1ade49240535535aa020232c8a4b1c114/modeling_chatglm.py#L1155

Remove this line. (This file resides in huggingface cache directory, by default it's ~/.cache/huggingface/)

Notice:

  • It runs purely on CPU so the inference is relatively slow (~30 seconds for a question like "你是谁?")
  • Memory usage is doubled (of the total model size), so expect to use around 24 ~ 25 GB.

On the long term I'd expect the authors to optimize it for Apple Silicon chips (use mps for better performance).

JokerQyou avatar Mar 15 '23 06:03 JokerQyou

But the problem still exist and got a text: Backend MacOSX is interactive backend. Turning interactive mode on. How to turn on?

Raphtalia-chann avatar Mar 15 '23 12:03 Raphtalia-chann

The model implementation without quantization does support MacOS. The error occurs because when you load a remote repo, transformers will check all the libraries imported in modeling_chatglm.py, including cpm_kernel, which doesn't support MacOS. But in fact, you don't need to import cpm_kernel if you don't use quantization. Since I don't have access to change either transformers or cpm_kernel, I can't fix it by my side. But the solution on the user side is also simple: just clone the repo locally (git clone https://huggingface.co/THUDM/chatglm-6b) and replace "THUDM/chatglm-6b" with your local repo path.

事实上,如果不需要量化的话,我们的模型实现是支持MacOS的。会出现报错的原因是transformers在加载一个远程仓库的时候会尝试加载modeling_chatglm.py中import的所有库,包括cpm_kernel。而cpm_kernel不支持MacOS。但是事实上如果你不会用到量化的话,import cpm_kernel这行代码根本不会被用到。因为我没有权限更改transformers或者cpm_kernel,所以我没有办法在我这边解决这个问题。但是在用户侧解决这个问题其实很简单,只要手动把仓库clone下来(git clone https://huggingface.co/THUDM/chatglm-6b),然后把demo里的"THUDM/chatglm-6b"替换成你本地的目录。

duzx16 avatar Mar 15 '23 13:03 duzx16

Has anyone been able to successfully run it on a Mac?

mice33 avatar Mar 16 '23 03:03 mice33

@mice33 I succeeded. image

jeffwcx avatar Mar 16 '23 04:03 jeffwcx

Hardware specifications are as follows:

MacBook Pro Model Identifier: Mac14,10 Chip: Apple M2 Pro Total Number of Cores: 12 (8 performance and 4 efficiency) Memory: 32 GB

My steps are as follows:

1. Download the model and modify the code

brew install git-lfs
# Clone to the `chatglm` folder
git clone https://huggingface.co/THUDM/chatglm-6b chatglm
cd chatglm
git lfs install
# This will take a long time
git lfs pull

Modify the modeling_chatglm.py file and comment out the following two lines:

--- a/modeling_chatglm.py
+++ b/modeling_chatglm.py
@@ -1166,6 +1166,6 @@ class ChatGLMForConditionalGeneration(ChatGLMPreTrainedModel):
         return torch.tensor(return_seqs, dtype=torch.long, device=kwargs['input_ids'].device)
 
     def quantize(self, bits: int):
-        from .quantization import quantize
-        self.transformer = quantize(self.transformer, bits)
+        # from .quantization import quantize
+        # self.transformer = quantize(self.transformer, bits)

2. Modify the demo code

 git clone https://github.com/THUDM/ChatGLM-6B.git
 cd ChatGLM-6B
 pip install -r requirements.txt
 pip install gradio

Modify web_demo.py

--- a/web_demo.py
+++ b/web_demo.py
@@ -1,8 +1,8 @@
 from transformers import AutoModel, AutoTokenizer
 import gradio as gr
 
-tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True)
-model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).half().cuda()
+tokenizer = AutoTokenizer.from_pretrained("../chatglm", trust_remote_code=True)
+model = AutoModel.from_pretrained("../chatglm", trust_remote_code=True).float()
 model = model.eval()

then

python web_demo.py

After successful execution, it will display:

 running on local URL:  http://127.0.0.1:7860

jeffwcx avatar Mar 16 '23 04:03 jeffwcx

Succeeded using @jeffwcx 's solution, thanks for sharing. I cloned the hugging face repo into the repositories folder. And the step 1 modifying part is not neccessary since we directly use the float() and not calling quantize() function locally so cpm_kernel won't be loaded.

But as mentioned above, pure CPU is really slow, looking forward to the mps solution. So the model would be pretty "portable" (lol

The first round takes 25 seconds and the second round takes nearly 4 minutes.

使用 jeffwcx 的方案成功了,感谢分享。因为是本地加载而且不调用量化方法,所以其实可以不用修改注释里的内容(省略第一步的后半部份)。 但是和上面提到的一样,纯 CPU 运算实在是太慢了(如下图所示,第一轮25s,第二轮将近4分钟),期待能有 mps 的方案,就能带着模型出去玩了 :)

MacBook Pro Model Identifier: Mac14,10 Chip: Apple M2 Max (12c32g)

image

noahzark avatar Mar 16 '23 06:03 noahzark

Tried to force using MPS backend, and got NotImplementedError: Unknown device for graph fuser. Seems warning MPS is not a fusable device

image

image

noahzark avatar Mar 16 '23 08:03 noahzark

# install nightly version of pytorch
pip3 install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cpu
// use mps backend
mps_device = torch.device("mps")
tokenizer = AutoTokenizer.from_pretrained("../chatglm", trust_remote_code=True)
model = AutoModel.from_pretrained("../chatglm", trust_remote_code=True).half()
model.to(mps_device)

The model can be initialized, but an error occurs when generating an output. It appears that some functions of PyTorch may be available on MacOS 13.3 Beta. I will try again later and hope that it will run successfully.

chaucerling avatar Mar 17 '23 09:03 chaucerling

After upgrading to MacOS 13.3 Beta4, I ran the cli demo but got the error NotImplementedError: Unknown device for graph fuser. This may be because the JIT fusion setting is not compatible with MPS. Therefore, I commented out the JIT setting in modeling_chatglm.py at line 34~37.

# flags required to enable jit fusion kernels
# torch._C._jit_set_profiling_mode(False)
# torch._C._jit_set_profiling_executor(False)
# torch._C._jit_override_can_fuse_on_cpu(True)
# torch._C._jit_override_can_fuse_on_gpu(True)

after running the code once aggain, I got another error invalid type: 'torch.mps.HalfTensor' at line 274. It can be fixed by editing line 268.

dtype = attention_scores.dtype # return torch.float16

After fixing above errors, the code can run without error occurred, but the output can not generate after waiting a long time. I still need some help to identify the issue.

python: 3.10.9 torch: 2.1.0.dev20230317 transformers: 4.26.1

chaucerling avatar Mar 17 '23 18:03 chaucerling

Hardware specifications are as follows:

MacBook Pro Model Identifier: Mac14,10 Chip: Apple M2 Pro Total Number of Cores: 12 (8 performance and 4 efficiency) Memory: 32 GB

My steps are as follows:

1. Download the model and modify the code

brew install git-lfs
# Clone to the `chatglm` folder
git clone https://huggingface.co/THUDM/chatglm-6b chatglm
cd chatglm
git lfs install
# This will take a long time
git lfs pull

Modify the modeling_chatglm.py file and comment out the following two lines:

--- a/modeling_chatglm.py
+++ b/modeling_chatglm.py
@@ -1166,6 +1166,6 @@ class ChatGLMForConditionalGeneration(ChatGLMPreTrainedModel):
         return torch.tensor(return_seqs, dtype=torch.long, device=kwargs['input_ids'].device)
 
     def quantize(self, bits: int):
-        from .quantization import quantize
-        self.transformer = quantize(self.transformer, bits)
+        # from .quantization import quantize
+        # self.transformer = quantize(self.transformer, bits)

2. Modify the demo code

 git clone https://github.com/THUDM/ChatGLM-6B.git
 cd ChatGLM-6B
 pip install -r requirements.txt
 pip install gradio

Modify web_demo.py

--- a/web_demo.py
+++ b/web_demo.py
@@ -1,8 +1,8 @@
 from transformers import AutoModel, AutoTokenizer
 import gradio as gr
 
-tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True)
-model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).half().cuda()
+tokenizer = AutoTokenizer.from_pretrained("../chatglm", trust_remote_code=True)
+model = AutoModel.from_pretrained("../chatglm", trust_remote_code=True).float()
 model = model.eval()

then

python web_demo.py

After successful execution, it will display:

 running on local URL:  http://127.0.0.1:7860

git lfs pull 了多少G内容? 我这磁盘都快满了

icowan avatar Mar 21 '23 08:03 icowan

我的Mac 16G 12c, 我从ubuntu:18.04镜像开始安装了docker chatglm-6b-int4:0.1, 设置了docker CPU, RAM参数,然后启动docker docker run -d --memory-swap -1 -v pwd:/workspace chatglm-6b-int4:0.1 可以启动,CPU拉满,SWAP也占了很多,速度惨不忍睹,可以跑出结果 good luck

Erickrus avatar Mar 21 '23 12:03 Erickrus

@icowan 大概26G

jeffwcx avatar Mar 21 '23 14:03 jeffwcx

有没有感觉,CPU没有并行计算,只有一个python占满CPU利用率,其他核心都是空跑闲置……

taozhijiang avatar Mar 22 '23 16:03 taozhijiang