日本熟妇hd丰满老熟妇,中文字幕一区二区三区在线不卡 ,亚洲成片在线观看,免费女同在线一区二区

Llama3-8B大模型微調訓練

DSW是一款交互式建模平臺,適合需要定制化微調模型并追求優化效果的開發者。本文以Llama-3-8B-Instruct模型為例,為您介紹如何在DSW中對Llama3大模型進行參數微調訓練,以使模型更好地理解和適應特定的任務,提高模型在指定任務上的表現和性能。

背景信息

Llama3是Meta于2024年4月開放的Llama系列的最新模型。基于超過15萬億tokens的訓練,相當于Llama2數據集的7倍之多。該模型不僅支持8K長文本,還配備了經過改進的Tokenizer,其詞匯量高達128K token,確保在處理復雜語境和專業術語時實現更精準高效的性能表現。

Llama3提供8B和70B兩個版本以滿足不同場景的需求,每個版本都包括基礎和指令調優兩種形式:

  • 8B版本

    適合消費級GPU環境,確保在有限計算資源下實現快速部署與開發工作,適用于對模型響應速度與成本效益要求較高的應用場景。

    • Meta-Llama-3-8b:8B基礎模型

    • Meta-Llama-3-8b-instruct:8B基礎模型的指令調優版

  • 70B版本

    面向大規模AI應用定制,憑借其龐大的參數規模,適合處理高復雜度任務和追求更優模型性能的高端項目。

    • Meta-Llama-3-70b:70B基礎模型

    • Meta-Llama-3-70b-instruct:70B基礎模型的指令調優版

前提條件

  • 創建工作空間,詳情請參見創建工作空間

  • 創建DSW實例,其中關鍵參數配置如下。具體操作,請參見創建DSW實例

    • 實例規格選擇:推薦使用V100(16GB)或更高顯存的GPU。

    • Python環境:建議使用Python3.9及以上版本。

    • 鏡像選擇:本方案在鏡像URL中輸入dsw-registry-vpc.REGION.cr.aliyuncs.com/pai-training-algorithm/llm_deepspeed_peft:v0.0.3,其中,REGION需要替換為DSW實例所屬地域對應的代號,例如cn-hangzhoucn-shanghai等,更多地域和REGION對應關系請參見下表。

      地域

      REGION代號

      杭州

      cn-hangzhou

      上海

      cn-shanghai

      北京

      cn-beijing

      深圳

      cn-shenzhen

  • 使用Llama3大模型前請閱讀Meta官方許可證

    說明

    如果無法訪問,您可能需要設置代理后再嘗試重新訪問。

步驟一、下載模型

方式一:在DSW下載模型

  1. 進入DSW開發環境。

    1. 登錄PAI控制臺

    2. 在頁面左上方,選擇DSW實例所在的地域。

    3. 在左側導航欄單擊工作空間列表,在工作空間列表頁面中單擊默認工作空間名稱,進入對應工作空間內。

    4. 在左側導航欄,選擇模型開發與訓練>交互式建模(DSW)

    5. 單擊需要打開的實例操作列下的打開,進入DSW實例開發環境。

  2. Launcher頁面中,單擊快速開始區域Notebook下的Python3

  3. 在Notebook中執行以下代碼下載模型文件。代碼會自動選擇適當的下載地址,并將模型文件下載至當前目錄。

    ! pip install modelscope==1.12.0 transformers==4.37.0
    from modelscope.hub.snapshot_download import snapshot_download
    snapshot_download('LLM-Research/Meta-Llama-3-8B-Instruct', cache_dir='.', revision='master')

方式二:在Meta下載模型

前往Meta申請下載模型

說明

如果無法訪問,您可能需要設置代理后再嘗試重新訪問。

步驟二、準備數據集

本案例準備了英文詩歌數據集,用于微調Llama 3模型,提高其生成詩歌的表現能力。在DSW的Notebook中執行以下命令,即可下載模型所需的訓練數據集。

!wget https://atp-modelzoo-sh.oss-cn-shanghai.aliyuncs.com/tutorial/llm_instruct/en_poetry_train.json

您也可以參考該數據集的格式,根據自己的使用場景,準備所需的數據集。通過微調,能夠提高大語言模型在特定任務上的回答準確性。

步驟三、微調模型

LoRA輕量化訓練

基于已有的訓練腳本/ml/code/sft.py進行模型的LoRA輕量化訓練。訓練結束之后,將模型參數進行量化,以便使用更少的顯存進行推理。

當運行accelerate launch命令時,會使用這些參數啟動指定的Python腳本,并且根據multi_gpu.yaml配置文件中的設置,在計算資源允許的范圍內進行訓練。

! accelerate launch --num_processes 1 --config_file /ml/code/multi_gpu.yaml /ml/code/sft.py \
    --model_name  ./LLM-Research/Meta-Llama-3-8B-Instruct/ \
    --model_type llama \
    --train_dataset_name chinese_medical_train_sampled.json \
    --num_train_epochs 3 \
    --batch_size 8 \
    --seq_length 128 \
    --learning_rate 5e-4 \
    --lr_scheduler_type linear \
    --target_modules k_proj o_proj q_proj v_proj \
    --output_dir lora_model/ \
    --apply_chat_template \
    --use_peft \
    --load_in_4bit \
    --peft_lora_r 32 \
    --peft_lora_alpha 32 

示例使用的參數說明如下,請您根據實際情況進行修改:

  • accelerate launch命令行工具用于在多GPU中啟動和管理深度學習訓練腳本。

    • num_processes 1:設置并行處理的進程數量為1,即不進行多進程并行處理。

    • config_file/ml/code/multi_gpu.yaml:指定配置文件的路徑。

    • /ml/code/sft.py:指定要運行的Python腳本的路徑。

  • 腳本/ml/code/sft.py接受的參數:

    • --model_name./LLM-Research/Meta-Llama-3-8B-Instruct/:指定預訓練模型的路徑。

    • --model_type llama:指定模型的類型,此處為Llama。

    • --train_dataset_namechinese_medical_train_sampled.json:指定訓練數據集的路徑。

    • --num_train_epochs 3:設置訓練的輪次為3。

    • --batch_size 8:設置批處理的大小為8。

    • --seq_length 128:設置序列的長度為128。

    • --learning_rate 5e-4:設置學習率為0.0005。

    • --lr_scheduler_type linear:設置學習率調度器類型為線性。

    • --target_modules k_proj o_proj q_proj v_proj:指定在微調中需要特別關注的模型模塊。

    • --output_dir lora_model/:指定輸出目錄路徑,微調后的模型將被保存在這里。

    • --apply_chat_template:指定訓練時應用聊天模板。

    • --use_peft:在訓練過程中使用參數有效調優PEFT(Parameter-Efficient Fine-Tuning)方法。

    • --load_in_4bit:指示模型權重載入時使用4位精度,減少內存消耗。

    • --peft_lora_r 32:如果使用了LoRA(Low-Rank Adaptation)作為參數有效調優方法的一部分,這會指定LoRA的值為32。

    • --peft_lora_alpha 32:設置LoRA參數的另一部分,alpha的大小為32。

將LoRA權重與基礎模型融合

執行以下命令,將LoRA權重與基礎模型融合。

! RANK=0 python /ml/code/convert.py \
    --model_name ./LLM-Research/Meta-Llama-3-8B-Instruct/ \
    --model_type llama \
    --output_dir trained_model/ \
    --adapter_dir lora_model/

示例使用的參數說明如下:

  • RANK=0:環境變量RANK用于分布式訓練中,表示當前進程在所有進程中的序號。設為0表明它是單進程或者是分布式訓練中的主進程。

  • python /ml/code/convert.py:執行convert.py腳本,用于權重轉換或其他轉換工作。

  • --model_name ./LLM-Research/Meta-Llama-3-8B-Instruct/:指定基礎模型的路徑。

  • --model_type llama:指定模型類型,此處為Llama。

  • --output_dir trained_model/:指定轉換后的模型和權重應該輸出保存的目錄。

  • --adapter_dir lora_model/:指定包含LoRA適配器權重的目錄。

步驟四、推理模型

執行以下命令進行模型推理,驗證微調效果。這里我們讓模型寫一首關于春天的詩歌:

import torch, transformers

# model_id = "./LLM-Research/Meta-Llama-3-8B-Instruct/"
model_id = "./trained_model/"
pipeline = transformers.pipeline(
    "text-generation",
    model=model_id,
    model_kwargs={"torch_dtype": torch.bfloat16},
    device="cuda",
)

messages = [
    {"role": "user", "content": "Write a poem on a topic 'spring' "},
]

prompt = pipeline.tokenizer.apply_chat_template(
        messages, 
        tokenize=False, 
        add_generation_prompt=True
)

terminators = [
    pipeline.tokenizer.eos_token_id,
    pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")
]

outputs = pipeline(
    prompt,
    max_new_tokens=1024,
    eos_token_id=terminators,
    do_sample=True,
    temperature=0.6,
    top_p=0.9,
)
print(outputs[0]["generated_text"][len(prompt):])

以下是模型生成的回答,可以看出微調后的模型在詩歌寫作方面表現很好:

Here's a poem on the topic of "Spring":

As winter's chill begins to fade,
The earth awakens from its shade,
And spring's sweet breath begins to blow,
Bringing life to all that's cold and slow.

The trees regain their vibrant hue,
And flowers bloom, both old and new,
Their petals dancing in the breeze,
As sunshine warms the world with ease.

The air is filled with sweet perfume,
As blossoms burst forth in their room,
And robins sing their morning song,
As spring's awakening is strong.

The world is fresh, and new, and bright,
As spring's warm light begins to take flight,
And all around, new life unfolds,
As winter's grip begins to grow old.

So let us bask in spring's warm rays,
And let our spirits soar and sway,
For in this season, we're reborn,
And all around, new life is sworn.

I hope you enjoy it!

步驟五、部署模型

您可以將微調后的模型權重上傳至OSS,使用EAS ChatLLM部署微調后的Llama3模型服務。具體操作,請參見5分鐘使用EAS一鍵部署LLM大語言模型應用

附錄:從DSW Gallery中運行Llama3

當前,DSW Gallery已經內預置Llama3的Notebook案例,您可以在DSW實例中打開案例并一鍵運行,或根據實際需求進行修改。具體操作,請參見Notebook Gallery

相關文檔

若要了解ChatLLM-WebUI的重要版本發布信息,請參見ChatLLM-WebUI版本發布詳情