当前位置:   article > 正文

colab使用本地数据集微调llama3-8b模型_colab微调千问模型

colab微调千问模型

        在Google的Colab上面采用unsloth,trl等库,训练数据集来自Google的云端硬盘,微调llama3-8b模型,进行推理验证模型的微调效果。

        保存模型到Google的云端硬盘可以下载到本地供其它使用。

准备工作:将训练数据集上传到google的云端硬盘根目录下,文件名就叫做train.json

train.json里面的数据格式如下:

[
  {
    "instruction": "你好",
    "output": "你好,我是智能助手胖胖"
  },
  {
    "instruction": "hello",
    "output": "Hello! I am 智能助手胖胖, an AI assistant developed by 丹宇码农. How can I assist you ?"
  }

......

]

采用unsloth库、trl库、transformers等库。

直接上代码:

  1. %%capture
  2. # Installs Unsloth, Xformers (Flash Attention) and all other packages!
  3. !pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
  4. !pip install --no-deps "xformers<0.0.26" trl peft accelerate bitsandbytes
  5. from unsloth import FastLanguageModel
  6. import torch
  7. max_seq_length = 2048 # Choose any! We auto support RoPE Scaling internally!
  8. dtype = None # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+
  9. load_in_4bit = True # Use 4bit quantization to reduce memory usage. Can be False.
  10. # 4bit pre quantized models we support for 4x faster downloading + no OOMs.
  11. fourbit_models = [
  12. "unsloth/mistral-7b-bnb-4bit",
  13. "unsloth/mistral-7b-instruct-v0.2-bnb-4bit",
  14. "unsloth/llama-2-7b-bnb-4bit",
  15. "unsloth/gemma-7b-bnb-4bit",
  16. "unsloth/gemma-7b-it-bnb-4bit", # Instruct version of Gemma 7b
  17. "unsloth/gemma-2b-bnb-4bit",
  18. "unsloth/gemma-2b-it-bnb-4bit", # Instruct version of Gemma 2b
  19. "unsloth/llama-3-8b-bnb-4bit", # [NEW] 15 Trillion token Llama-3
  20. ] # More models at https://huggingface.co/unsloth
  21. model, tokenizer = FastLanguageModel.from_pretrained(
  22. model_name = "unsloth/llama-3-8b-bnb-4bit",
  23. max_seq_length = max_seq_length,
  24. dtype = dtype,
  25. load_in_4bit = load_in_4bit,
  26. # token = "hf_...", # use one if using gated models like meta-llama/Llama-2-7b-hf
  27. )
  28. model = FastLanguageModel.get_peft_model(
  29. model,
  30. r = 16, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128
  31. target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
  32. "gate_proj", "up_proj", "down_proj",],
  33. lora_alpha = 16,
  34. lora_dropout = 0, # Supports any, but = 0 is optimized
  35. bias = "none", # Supports any, but = "none" is optimized
  36. # [NEW] "unsloth" uses 30% less VRAM, fits 2x larger batch sizes!
  37. use_gradient_checkpointing = "unsloth", # True or "unsloth" for very long context
  38. random_state = 3407,
  39. use_rslora = False, # We support rank stabilized LoRA
  40. loftq_config = None, # And LoftQ
  41. )
  42. alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
  43. ### Instruction:
  44. {}
  45. ### Input:
  46. {}
  47. ### Response:
  48. {}"""
  49. EOS_TOKEN = tokenizer.eos_token # Must add EOS_TOKEN
  50. def formatting_prompts_func(examples):
  51. instructions = examples["instruction"]
  52. outputs = examples["output"]
  53. texts = []
  54. for instruction, output in zip(instructions, outputs):
  55. input = ""
  56. # Must add EOS_TOKEN, otherwise your generation will go on forever!
  57. text = alpaca_prompt.format(instruction, input, output) + EOS_TOKEN
  58. texts.append(text)
  59. return { "text" : texts, }
  60. pass
  61. from datasets import load_dataset
  62. #dataset = load_dataset("yahma/alpaca-cleaned", split = "train")
  63. #dataset = dataset.map(formatting_prompts_func, batched = True,)
  64. from google.colab import drive
  65. # 挂载云端硬盘,加载成功后,在左边的文件树中将会多一个 /content/drive/MyDrive/ 目录
  66. drive.mount('/content/drive')
  67. # 加载本地数据集:
  68. # 有instruction和output,input为空字符串
  69. from datasets import load_dataset
  70. data_home = r"/content/drive/MyDrive/"
  71. data_dict = {
  72. "train": os.path.join(data_home, "train.json"),
  73. #"validation": os.path.join(data_home, "dev.json"),
  74. }
  75. dataset = load_dataset("json", data_files=data_dict, split = "train")
  76. print(dataset[0])
  77. dataset = dataset.map(formatting_prompts_func, batched = True,)
  78. from trl import SFTTrainer
  79. from transformers import TrainingArguments
  80. trainer = SFTTrainer(
  81. model = model,
  82. tokenizer = tokenizer,
  83. train_dataset = dataset,
  84. dataset_text_field = "text",
  85. max_seq_length = max_seq_length,
  86. dataset_num_proc = 2,
  87. packing = False, # Can make training 5x faster for short sequences.
  88. args = TrainingArguments(
  89. per_device_train_batch_size = 2,
  90. gradient_accumulation_steps = 4,
  91. warmup_steps = 5,
  92. max_steps = 60,
  93. learning_rate = 2e-4,
  94. fp16 = not torch.cuda.is_bf16_supported(),
  95. bf16 = torch.cuda.is_bf16_supported(),
  96. logging_steps = 1,
  97. optim = "adamw_8bit",
  98. weight_decay = 0.01,
  99. lr_scheduler_type = "linear",
  100. seed = 3407,
  101. output_dir = "outputs",
  102. ),
  103. )
  104. # 开始微调训练
  105. trainer_stats = trainer.train()
  106. #推理
  107. # alpaca_prompt = Copied from above
  108. FastLanguageModel.for_inference(model) # Enable native 2x faster inference
  109. inputs = tokenizer(
  110. [
  111. alpaca_prompt.format(
  112. "你是谁?", # instruction
  113. "", # input
  114. "", # output - leave this blank for generation!
  115. )
  116. ], return_tensors = "pt").to("cuda")
  117. outputs = model.generate(**inputs, max_new_tokens = 64, use_cache = True)
  118. tokenizer.batch_decode(outputs)
  119. #此处输出的答案,能明显看到就是自己训练的数据,而不是原来模型的输出。说明微调起作用了
  120. # 保存模型,改成挂接的云硬盘目录也可以保存到google的个人云存储空间,然后打开个人云存储空间下载到本地
  121. model.save_pretrained("lora_model") # Local saving
  122. tokenizer.save_pretrained("lora_model")
  123. # Merge to 16bit
  124. if True: model.save_pretrained_merged("model", tokenizer, save_method = "merged_16bit",)

其实可以将.ipynb文件上传到个人云存储空间,双击这个文件就会打开colab,然后依次执行代码即可,随时可以增加、删除、修改,特别方便,还能免费使用GPU、CPU等资源,真的是广大AI爱好者的不错选择。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/899376
推荐阅读
相关标签
  

闽ICP备14008679号