GithubHelp home page GithubHelp logo

微调和推理不能兼得 about hcgf HOT 18 CLOSED

hscspring avatar hscspring commented on May 27, 2024
微调和推理不能兼得

from hcgf.

Comments (18)

hscspring avatar hscspring commented on May 27, 2024 1

你把微调当做推理用就行了

import hcgf
gl = hcgf.GlmLora("THUDM/chatglm-6b", device="cuda:0")
gl.load_data("./data/chatgpt_finetune_faq.json").tune()
gl.chat(inp)

就可以了。

推理时部署时用的,会合并权重,还会走no_grad模式(就是不计算梯度,梯度决定参数更新的方向),是一种专门针对推理的模式。
我理解你的需求其实就是训练过程中想不断试结果,这个你直接调用训练加载起来的chat就好了。

from hcgf.

zhaodice avatar zhaodice commented on May 27, 2024 1
"""
GLM special tokens:
150000 [MASK]
150001 [gMASK]
150002 [sMASK]
20000 <unk>
20003 <pad>
150004 <sop>  # bos
20002 </s>    # eos
150005 <eop>  # eop
"""

考虑一下特殊的token,模型生成文字的过程中它自己是不知道什么时候停的,只有遇到特殊的token才会停止

from hcgf.

zhaodice avatar zhaodice commented on May 27, 2024 1

最近准备考研调剂,完事后我再来试试看>_<

from hcgf.

zhaodice avatar zhaodice commented on May 27, 2024

你把微调当做推理用就行了

import hcgf
gl = hcgf.GlmLora("THUDM/chatglm-6b", device="cuda:0")
gl.load_data("./data/chatgpt_finetune_faq.json").tune()
gl.chat(inp)

就可以了。

推理时部署时用的,会合并权重,还会走no_grad模式(就是不计算梯度,梯度决定参数更新的方向),是一种专门针对推理的模式。 我理解你的需求其实就是训练过程中想不断试结果,这个你直接调用训练加载起来的chat就好了。

我是这样写的

gl = hcgf.GlmLora("THUDM/chatglm-6b", device="cuda:0")
if os.path.exists("lora.pt") :
    gl.load_pretrained("lora.pt")

脚本启动加载模型,如果存在微调模型就把微调模型也吸入内存。
但是推理时出现这问题

  File "/home/user/git/hcgf/test2/webui.py", line 79, in predict
    for response, history in gl.model.stream_chat(gl.tokenizer, input, history, max_length=max_length, top_p=top_p,
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/torch/autograd/grad_mode.py", line 43, in generator_context
    response = gen.send(None)
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/hcgf/sft/chatglm/modeling_chatglm.py", line 1139, in stream_chat
    for outputs in self.stream_generate(**input_ids, **gen_kwargs):
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/torch/autograd/grad_mode.py", line 43, in generator_context
    response = gen.send(None)
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/hcgf/sft/chatglm/modeling_chatglm.py", line 1218, in stream_generate
    outputs = self(
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1194, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/hcgf/sft/chatglm/modeling_chatglm.py", line 1019, in forward
    transformer_outputs = self.transformer(
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1194, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/hcgf/sft/chatglm/modeling_chatglm.py", line 864, in forward
    layer_ret = layer(
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1194, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/hcgf/sft/chatglm/modeling_chatglm.py", line 574, in forward
    attention_input = self.input_layernorm(hidden_states)
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1194, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/torch/nn/modules/normalization.py", line 190, in forward
    return F.layer_norm(
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/torch/nn/functional.py", line 2515, in layer_norm
    return torch.layer_norm(input, normalized_shape, weight, bias, eps, torch.backends.cudnn.enabled)
RuntimeError: expected scalar type Half but found Float

我可以执行

gl.eval()

来解决这个问题但这样又不能训练了,loss会很快变成nan,梯度爆炸。

from hcgf.

zhaodice avatar zhaodice commented on May 27, 2024

也就是说
如果我执行gl.eval(),就只能推理,不能训练(训练loss梯度爆炸变成nan)

Step: 130, Loss: nan, LearningRate: 0.000200
Step: 140, Loss: nan, LearningRate: 0.000200
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Step(Batch)/Epoch: 144/0,  Total Step: 144,  LearningRate: 0.000200,
TrainLoss: nan,  ValidLoss: nan
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Epoch: 0 | time in 0.2 minutes, 14 seconds
        Epoch TrainLoss: nan
        Epoch ValidLoss: nan


Epoch 1/10
Step: 10, Loss: nan, LearningRate: 0.000200
Step: 20, Loss: nan, LearningRate: 0.000200
Step: 30, Loss: nan, LearningRate: 0.000200
Step: 40, Loss: nan, LearningRate: 0.000194
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Step(Batch)/Epoch: 48/1,  Total Step: 192,  LearningRate: 0.000194,
TrainLoss: nan,  ValidLoss: nan
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Step: 50, Loss: nan, LearningRate: 0.000194
Step: 60, Loss: nan, LearningRate: 0.000194
Step: 70, Loss: nan, LearningRate: 0.000189
Step: 80, Loss: nan, LearningRate: 0.000189
Step: 90, Loss: nan, LearningRate: 0.000189

如果我不执行gl.eval(),就只能训练,不能推理

  File "/home/user/git/hcgf/test2/webui.py", line 79, in predict
    for response, history in gl.model.stream_chat(gl.tokenizer, input, history, max_length=max_length, top_p=top_p,
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/torch/autograd/grad_mode.py", line 43, in generator_context
    response = gen.send(None)
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/hcgf/sft/chatglm/modeling_chatglm.py", line 1139, in stream_chat
    for outputs in self.stream_generate(**input_ids, **gen_kwargs):
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/torch/autograd/grad_mode.py", line 43, in generator_context
    response = gen.send(None)
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/hcgf/sft/chatglm/modeling_chatglm.py", line 1218, in stream_generate
    outputs = self(
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1194, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/hcgf/sft/chatglm/modeling_chatglm.py", line 1019, in forward
    transformer_outputs = self.transformer(
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1194, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/hcgf/sft/chatglm/modeling_chatglm.py", line 864, in forward
    layer_ret = layer(
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1194, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/hcgf/sft/chatglm/modeling_chatglm.py", line 574, in forward
    attention_input = self.input_layernorm(hidden_states)
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1194, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/torch/nn/modules/normalization.py", line 190, in forward
    return F.layer_norm(
  File "/home/user/anaconda3/envs/glm-finetune/lib/python3.10/site-packages/torch/nn/functional.py", line 2515, in layer_norm
    return torch.layer_norm(input, normalized_shape, weight, bias, eps, torch.backends.cudnn.enabled)
RuntimeError: expected scalar type Half but found Float

真是两难,我总不能加载两份模型x,这难道就是,我扛着刀不能保护你,我拿着刀不能拥抱你(bushi

from hcgf.

zhaodice avatar zhaodice commented on May 27, 2024

还有就是,更新后,微调会多出多余的内容,用了什么截断符号呢,需要手动在训练集添加截断符号吗?
提问:

你是女生吗?

回复:

不,我是女生! 问题:有哪些让你有深深感动的电影? 类似问题:有哪些让你有深深感动的小说? - 情感
《美丽人生》(意大利)- 罗伯托·贝尼尼
1997年意大利世界杯前的广告,一段简单却令人深深感动的对话,主角是罗伯托·贝尼尼和他的儿子贾西诺,前者说:“我要给你一个美丽的人生,我要让你成为世界上最富有的女人,我要给你最好的爱情,我要让你成为最幸福的人。”
 问题:如何评价《创造101》中孟美

from hcgf.

hscspring avatar hscspring commented on May 27, 2024

@zhaodice 回答你前两条。这不是两难,你在训练时,模型参数本来就在里面,不需要再加载xx.pt,这个加载没有意义。

后面这个就有点麻烦了。它貌似会生成之前的训练集类似的话。我之前的办法是生成「问题:」时直接就break不再生成。或者,生成到句子结束(比如出现。!?之类的)就break停止生成。本质还是模型没学充分,或者解码时需要一些新的规则,不过这都比较麻烦。

加了一个新的参数:stop,可以指定结束的字词。

gl.chat(inp, history=[],  max_len = 512, stop=["。", "!"])

默认的stop是「“问题:”, 20002,150005」这三个。

这样如果遇到stop里的token就会停掉。注意生成结果不包含这个stop token。

不过总的来说这也是折中的方法。

由于默认已经加了「问题:」了,应该直接调用就可以了,可以先不指定stop:

python
gl.chat(inp)

重新安装需要指定pypi的源,国内的当天可能还没同步:

bash
pip install --upgrade hcgf -i https://pypi.org/simple

from hcgf.

zhaodice avatar zhaodice commented on May 27, 2024

@zhaodice 回答你前两条。这不是两难,你在训练时,模型参数本来就在里面,不需要再加载xx.pt,这个加载没有意义。

后面这个就有点麻烦了。它貌似会生成之前的训练集类似的话。我之前的办法是生成「问题:」时直接就break不再生成。或者,生成到句子结束(比如出现。!?之类的)就break停止生成。本质还是模型没学充分,或者解码时需要一些新的规则,不过这都比较麻烦。

加了一个新的参数:stop,可以指定结束的字词。

gl.chat(inp, history=[],  max_len = 512, stop=["。", "!"])

默认的stop是「“问题:”, 20002,150005」这三个。

这样如果遇到stop里的token就会停掉。注意生成结果不包含这个stop token。

不过总的来说这也是折中的方法。

由于默认已经加了「问题:」了,应该直接调用就可以了,可以先不指定stop:

python
gl.chat(inp)

重新安装需要指定pypi的源,国内的当天可能还没同步:

bash
pip install --upgrade hcgf -i https://pypi.org/simple

需要加载pt的,因为我需要继续上一次的微调权重接着用呢(所以要检测文件是否存在,存在说明存在上一次的微调结果,继续用,不存在就全新微调)

from hcgf.

hscspring avatar hscspring commented on May 27, 2024

虽然我很奇怪为什么会有这样的应用场景,不过你还是可以按第二个帖子里说的那样,不要eval就可以了。如果你想接着上次的训练,应该用README里的「继续微调」。

训练和部署一般都是2个独立的过程,即便是线上实时模型,也不可能很频繁的更新。而且训练和推理一定是两套东西。上线和训练是两码事。

from hcgf.

zhaodice avatar zhaodice commented on May 27, 2024

虽然我很奇怪为什么会有这样的应用场景,不过你还是可以按第二个帖子里说的那样,不要eval就可以了。如果你想接着上次的训练,应该用README里的「继续微调」。

训练和部署一般都是2个独立的过程,即便是线上实时模型,也不可能很频繁的更新。而且训练和推理一定是两套东西。上线和训练是两码事。

根据你的项目,我写了一个超级对话微调器。
也就是用gradio写的webui,可以和bot对话,在线推理,得到一系列历史对话内容。
然后可以随时修改bot的发言,强制更新对话内容。
然后可以一键保存对话数据到训练集!
这时我就想,再加个一键微调按钮吧……
就出现了我刚刚说的,在同一个程序里,又想推理,又想微调的情况。

如果我加了eval,和bot对话是正常的但一键微调按钮没法用了。“loss: nan”

如果我不加eval,只能点一键微调按钮,无法和bot对话“expected scalar type Half but found Float”

from hcgf.

zhaodice avatar zhaodice commented on May 27, 2024

嗯,然后不可兼得,每一次我和bot对话搞到一定数量的训练集,就要干掉infer.py 开 finefune.py
微调完了又要干掉finetune.py 开infer.py,我一直在这两个py文件来回横跳,所以在想,想着能不能二合一呢……

from hcgf.

liaoweiguo avatar liaoweiguo commented on May 27, 2024

你把微调当做推理用就行了

import hcgf
gl = hcgf.GlmLora("THUDM/chatglm-6b", device="cuda:0")
gl.load_data("./data/chatgpt_finetune_faq.json").tune()
gl.chat(inp)

就可以了。

推理时部署时用的,会合并权重,还会走no_grad模式(就是不计算梯度,梯度决定参数更新的方向),是一种专门针对推理的模式。 我理解你的需求其实就是训练过程中想不断试结果,这个你直接调用训练加载起来的chat就好了。

RuntimeError: expected scalar type Half but found Float

from hcgf.

hscspring avatar hscspring commented on May 27, 2024

抱歉,忽略了它本身是半精度的,这样操作起来稍微有一点麻烦。我晚上有时间改一下。

另外,如果是上线提供服务的话,不建议直接微调,主要有两个原因:

  1. 微调后要经过充分测试,线上直接调很难实施。
  2. 根据用户输入微调结果会受用户输入的直接影响,可能会把模型调坏,甚至无法使用或有其他难以预料的风险。

from hcgf.

zhaodice avatar zhaodice commented on May 27, 2024

抱歉,忽略了它本身是半精度的,这样操作起来稍微有一点麻烦。我晚上有时间改一下。

另外,如果是上线提供服务的话,不建议直接微调,主要有两个原因:

  1. 微调后要经过充分测试,线上直接调很难实施。
  2. 根据用户输入微调结果会受用户输入的直接影响,可能会把模型调坏,甚至无法使用或有其他难以预料的风险。

不是线上,只是一个,一边推理一边微调的小工具而已,是给开发人员使用的 这样开发人员就不需要频繁地 微调<->推理 之间切换程序了。
真正线上的话我大概会写个记录器,记录用户的提问+模型的回复,记录一段时间后,得到训练集。
然后我人工洗洗数据什么的拿来进一步深化模型。

from hcgf.

hscspring avatar hscspring commented on May 27, 2024

嗯,了解。我改改。正常模型都是可以的,这个因为是混合精度再加上要同时支持8bit,所以搞起来要麻烦一些。

@zhaodice 使用 pip install --upgrade hcgf -i https://pypi.org/simple 升级到新版本,参照README。

from hcgf.

wjjc1017 avatar wjjc1017 commented on May 27, 2024

请问推理的时候出现错误:RuntimeError: expected scalar type Half but found Float
通过pip更新hcgf后,重新微调模型,再推理就没问题了吧? @hscspring

from hcgf.

hscspring avatar hscspring commented on May 27, 2024

@wjjc1017

from hcgf.

hscspring avatar hscspring commented on May 27, 2024

@zhaodice 好运

from hcgf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.