BUG总结——A6000部署+微调GLM4-9b-chat模型

1.在安装了basic_demo 和 fineturne_demo的工具包,执行微调指令发现如下错误

TypeError: Seq2SeqTrainer.training_step() takes 3 positional arguments but 4 were given
Exception in thread Thread-3 (_pin_memory_loop):
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.10/threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.10/site-packages/torch/utils/data/_utils/pin_memory.py", line 59, in _pin_memory_loop
    do_one_step()
 

我的解决方案:

原本transformers==4.46.2

pip install transformers==4.44.0

2.在后续微调中出现loss 为0的情况

我确定在执行过程中,使用的是bf16 

由于glm的官方提示词,无法满足本任务需要,本人重新设计了提示词。

或许是因为提示词设计的过长,导致上述问题。

即 模型一次处理的输入文本的长度上限过短,模型会截断部分输入数据,导致其无法生成有效的标签

将最大输入输出长度从512修改为1024

解决上述问题

<think>好的,用户想要使用unsloth来微调deepseek-r1-8B模型,并希望得到完整的代码。首先,我需要确认unsloth是否支持这个模型。unsloth主要用于优化微调过程,比如减少内存占用和加快训练速度,通常与Hugging Face的transformers库结合使用。deepseek-r1-8B可能是一个基于类似LLaMA架构的模型,所以可能需要检查是否支持。 接下来,用户可能需要代码示例,包括数据准备、模型加载、训练配置、训练过程和保存模型。需要确保步骤完整,例如安装必要的库,加载数据集,使用正确的模型名称,设置训练参数如batch size、learning rate等,以及处理可能的内存问题,比如使用4位量化。 另外,用户可能对数据格式有疑问,所以需要提供一个示例数据集的结构,比如alpaca格式。还要注意可能出现的错误,比如OOM(内存不足),这时候可能需要调整batch size或者使用梯度累积。 需要提醒用户替换数据集路径,并根据硬件调整参数,比如device_map和dtype设置。最后,保存模型并演示如何加载使用,确保代码的可运行性。 可能还需要验证代码的准确性,比如检查unsloth的最新文档,确认支持的模型类型,以及是否有额外的参数需要注意。同时,确保训练器的设置正确,比如使用Seq2SeqTrainer,因为可能是生成任务。 总结:步骤包括安装库、准备数据、加载模型和分词器、配置训练参数、训练、保存模型。需要注意模型兼容性、硬件限制和参数调整,确保用户能顺利运行代码。</think>以下是一个使用Unsloth微调DeepSeek-R1-8B模型的完整代码示例。请注意根据您的硬件调整参数(如batch_size、gradient_accumulation_steps等): ```python # 首先安装必要库(建议使用Python 3.10+) # pip install "unsloth[colab] @ git+https://github.com/unslothai/unsloth.git" # pip install --no-deps "xformers<0.26.0" trl peft accelerate datasets bitsandbytes import torch from unsloth import FastLanguageModel from datasets import load_dataset from trl import SFTTrainer from transformers import TrainingArguments # 参数配置 max_seq_length = 2048 # 根据GPU内存调整 dtype = None # None自动检测,或指定为torch.float16/torch.bfloat16 load_in_4bit = True # 使用4位量化节省内存 # 加载DeepSeek-R1-8B模型 model, tokenizer = FastLanguageModel.from_pretrained( model_name = "deepseek-ai/deepseek-llm-7b-r1", # 确认HuggingFace模型名称 max_seq_length = max_seq_length, dtype = dtype, load_in_4bit = load_in_4bit, ) # 添加LoRA适配器(可选) model = FastLanguageModel.get_peft_model( model, r=16, # LoRA秩 target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj"], # 常见目标模块 lora_alpha=16, lora_dropout=0, bias="none", use_gradient_checkpointing=True, ) # 准备数据集(示例使用Alpaca格式) dataset = load_dataset("yahma/alpaca-cleaned", split="train") dataset = dataset.map(lambda sample: { "text": f"""Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: {sample[&#39;instruction&#39;]} ### Input: {sample[&#39;input&#39;]} ### Response: {sample[&#39;output&#39;]}""" }) # 训练参数配置 trainer = SFTTrainer( model = model, tokenizer = tokenizer, train_dataset = dataset, dataset_text_field = "text", max_seq_length = max_seq_length, dataset_num_proc = 2, packing = True, # 序列打包提高效率 args = TrainingArguments( per_device_train_batch_size = 2, gradient_accumulation_steps = 4, warmup_steps = 5, num_train_epochs = 3, learning_rate = 2e-5, fp16 = not torch.cuda.is_bf16_supported(), bf16 = torch.cuda.is_bf16_supported(), logging_steps = 1, optim = "adamw_8bit", weight_decay = 0.01, lr_scheduler_type = "linear", seed = 3407, output_dir = "outputs", save_strategy="steps", save_steps=500, ), ) # 执行训练 trainer.train() # 保存微调后的模型 model.save_pretrained("lora_adapter") # 保存LoRA适配器 # model.push_to_hub("your_username/model_name") # 可选:上传到HuggingFace Hub # 使用微调后的模型生成文本 inputs = tokenizer( ["### Instruction:\nExplain deep learning in 3 sentences\n\n### Response:"], return_tensors="pt" ).to("cuda") outputs = model.generate(**inputs, max_new_tokens=128) print(tokenizer.decode(outputs[0], skip_special_tokens=True)) ``` 注意事项: 1. 硬件要求:至少需要24GB显存(建议A100/A6000等) 2. 数据集:示例使用Alpaca格式,请替换为您的实际数据集 3. 模型名称:请确认HuggingFace上的准确模型名称 4. 内存优化:如遇OOM错误,可以尝试: - 降低`per_device_train_batch_size` - 增加`gradient_accumulation_steps` - 使用`load_in_4bit=True` - 减小`max_seq_length` 建议在微调前: 1. 检查CUDA可用性:`print(torch.cuda.is_available())` 2. 验证模型加载:`print(model)` 3. 测试基本推理:先运行生成代码确保基础模型正常工作 如需完整生产级实现,建议参考: - Unsloth官方文档:https://github.com/unslothai/unsloth - DeepSeek官方微调指南:https://huggingface.co/deepseek-ai
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值