少女祈祷中...

使用OpenRLHF
https://github.com/OpenRLHF/OpenRLHF?tab=readme-ov-file
的过程.


基本上是按照README的知道一步步来.
https://github.com/OpenRLHF/OpenRLHF?tab=readme-ov-file

零. 大战 nvidia-docker

To use OpenRLHF, first launch the docker container (Recommended) and pip install openrlhf inside the docker container:

由于之前已经安装好了docker, 所以主要是在搞nvidia-docker的东西.

过程不算轻松. 具体可见另一篇blog.

一. 大战 pip install openrlhf

根据README的指导:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Launch the docker container
docker run --runtime=nvidia -it --shm-size="8g" --name docker-OpenRLHF --cap-add=SYS_ADMIN -v $PWD:/openrlhf nvcr.io/nvidia/pytorch:24.07-py3 bash
pip uninstall xgboost transformer_engine flash_attn -y

# pip install
pip install openrlhf

# If you want to use vLLM acceleration (Install vLLM 0.6.4.post1)
pip install openrlhf[vllm]
# latest vLLM is also supported
pip install openrlhf[vllm_latest]

# pip install the latest version
pip install git+https://github.com/OpenRLHF/OpenRLHF.git

# Or git clone
git clone https://github.com/OpenRLHF/OpenRLHF.git
cd OpenRLHF
pip install -e .

安装镜像源, 运行容器, pip install openrlhf 即可. 在搞定nvidia-docker的前提下不会遇到什么问题.

和README里的命令有一些不同. 1. 添加了--name的选项, 给容器命名. 2. 去除了-rm选项, 对容器进行复用 3. docker 内部的命令不需要 sudo


发现始终无法完成 命令, 一直卡在 Building wheel for flash-attn (setup.py) ... -这一步.

尝试按照README上的指引重新安装了nvidia-docker (需要把之前的docker给删掉, 算是白折腾了). 这一次倒是没有遇到网络问题, 一路把镜像源下载了下来. 但是进去之后还是卡在了 flash-attn这一步.

中间还遇到了

1
2
3
4
5
6
sudo apt-get update 

N: Ignoring file 'skypeforlinux.lis' in directory '/etc/apt/sources.list.d/' as it has an invalid filename extension
E: Conflicting values set for option Signed-By regarding source https://download.docker.com/linux/ubuntu/ focal: /usr/share/keyrings/docker-ce-archive-keyring.gpg != /usr/share/keyrings/docker-archive-keyring.gpg
E: Conflicting values set for option Signed-By regarding source https://nvidia.github.io/libnvidia-container/stable/ubuntu18.04/amd64/ /: /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg !=
E: The list of sources could not be read.

之类的问题. 找了找发现是两次安装导致密钥重复了. 这和gpt的解决方案是一样的. 只需要进入
/etc/apt/sources.list.d/, 然后把 nvidia-container-toolkit.list中的重复内容剔除, 只剩下一个就行.
https://github.com/NVIDIA/nvidia-docker/issues/1626

后面试着解决进度条不动的问题, 找了两个方法都不奏效. 试着单独安装 pip install flash-attn 也是同样的卡住了.
https://github.com/Dao-AILab/flash-attention/issues/224
https://github.com/adithya-s-k/omniparse/issues/36

后面找到了一个网页 https://til.simonwillison.net/python/installing-flash-attention, 上面貌似表示安装这个东西卡顿是个正常现象. (For some reason attempting to install this runs a compilation process which can take multiple hours.). 所以我打算给它一些时间. 只要它没有报错就让它在那跑吧.

二. Prepare Datasets

OPENRLHF整起来很没头绪. 一部分原因在于这玩意真的没什么资料, 国内的尚且不提, 国外的都没多少; 另一部分原因在于很不熟悉docker.

不过今天又着手开始折腾了一下, 渐渐有点眉目了.

Docker相关

可以直接把docker当成一个虚拟机 / 操作系统. 只不过它没有图形化界面罢了.
其中run命令就相当于安装系统, 而start命令相当于开机.

使用类似

1
docker exec docker-OpenRLHF ls

就可以在运行的容器中执行命令了.

通过类似

1
docker cp ./test.py docker-OpenRLHF:/workspace/codes

就可以在本机和容器之间传递文件

当然, 也可以直接运行

1
docker exec -it docker-OpenRLHF /bin/bash

此处 -i指令使容器保持标准输入流 stdin打开, 而-t分配一个伪终端, 两者结合就生成了一个交互式Shell, 从而能够执行各种shell命令.

之后我们就可以正常地在里面 pip install, 配好各种环境, 运行python代码等等. 只不过代码编辑的工作需要在本机上完成.

通过

1
2
docker start docker-OpenRLHF
docker stop docker-OpenRLHF

来运行 / 停止一个容器


Hugging Face / Transformers 相关

这两个东西密不可分. 使用方法如下:

先运行

1
pip install transformers

然后运行下面的python文件

1
2
3
4
5
6
7
8
9
10
11
from transformers import AutoTokenizer, AutoModelForSequenceClassification

model_name = "bert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

text = "hello world!"
inputs = tokenizer(text, return_tensors="pt")

outputs = model(**inputs)
print(outputs.logits)

如果一切正常就会看到输出.

但是要是有这么顺利就好了. 我遇到的第一个问题是网络链接问题.

1
2
OSError: We couldn't connect to 'https://huggingface.co' to load this file, couldn't find it in the cached files and it looks like bert-base-uncased is not the path to a directory containing a file named config.json.
Checkout your internet connection or see how to run the library in offline mode at 'https://huggingface.co/docs/transformers/installation#offline-mode'.

查找了一下, 有两个解决方法

  1. 把模型文件下载到本地, 然后更改相应的路径跑程序. 此处没有尝试这个方法. https://blog.csdn.net/m0_46295727/article/details/133221439
  2. 采用镜像源: 在运行程序前, 设置环境变量 export HF_ENDPOINT=https://hf-mirror.com https://github.com/adithya-s-k/omniparse/issues/6

于是加了个环境变量作为镜像源, 问题立刻解决

1
2
3
4
5
6
7
8
9
10
11
12
root@285efd5f88fe:/workspace# export HF_ENDPOINT=https://hf-mirror.com
root@285efd5f88fe:/workspace# python codes/test.py
tokenizer_config.json: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 48.0/48.0 [00:00<00:00, 291kB/s]
config.json: 570B [00:00, 1.30MB/s]
vocab.txt: 232kB [00:00, 901kB/s]
tokenizer.json: 466kB [00:00, 1.11MB/s]
model.safetensors: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 440M/440M [06:27<00:00, 1.14MB/s]
Some weights of BertForSequenceClassification were not initialized from the model checkpoint at bert-base-uncased and are newly initialized: ['classifier.bias', 'classifier.weight']
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
Some weights of BertForSequenceClassification were not initialized from the model checkpoint at bert-base-uncased and are newly initialized: ['classifier.bias', 'classifier.weight']
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
tensor([[-0.1272, 0.0683]], grad_fn=<AddmmBackward0>)

至此实例的模型都能跑了, 但是在网站上寻找到的模型还不知道要怎么用. 此处试了一个模型, 遇到了各种报错.

开始是一前一后两个库缺失的报错, 分别pip上去了.

1
2
pip install tiktoken
pip install blobfile

之后还是不行, 报错信息显示问题似乎出在模型的内部. 于是打算先跑个别的模型看看.

尝试跑了GPT2. 成功运行. 可惜gpt2只能胡言乱语.


在Docker上访问huggingface跑OPENRLHF

第一个问题是很多库没装上, 例如下面两个报错

1
2
3
4
5
6
7
8
9
10
++ python -m openrlhf.cli.serve_rm --reward_pretrain OpenRLHF/Llama-3-8b-rm-700k --port 5000 --bf16 --flash_attn --normalize_reward --max_len 8192 --batch_size 16
Traceback (most recent call last):
File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/usr/local/lib/python3.10/dist-packages/openrlhf/cli/serve_rm.py", line 5, in <module>
import uvicorn
ModuleNotFoundError: No module named 'uvicorn'

ModuleNotFoundError: No module named 'fastapi'

直接pip install 了相关的库之后就解决了.

之后还是没跑成功.


后续我也忘了究竟跑没跑起来了, 总之OPENRLHF貌似是一个陷阱. 对于初学者而言还是尝试别的框架比较好, 例如unslot