LLM finetuning handson JP
Created on December 21|Last edited on March 21
Comment
このレポートでは、W&Bを用いたLLMのファインチューニングプロセスについて説明します。また、LLMのファインチューニングの無料トレーニングコースがあります。 "Training and Fine-tuning Large Language Models (LLMs)". こちらもご確認ください。
Wandb setup
1. Wandb login

2. 101 of experiment tracking and W&B dashboard
環境変数としてキーを登録すると、APIキーを毎回入力することなくログインできます。
3. Team collaboration

W&Bの実験管理は主に、entity => project => runの順序で管理されます。entityはチーム単位です。デフォルトでは個人entityとして設定されていますが、チームentityを作成し、チーム内で同じprojectを管理することもできます。ただし、個人または学術利用の場合、個人のもの以外に1つのentity にのみ参加することができます。entity の下の単位がproject です。名前が示す通り、一つのMLまたはDL project に使用してください。project 内で多くの実験を実行する必要があり、 runはproject の下で管理されます。entity とプロジェクトは手動で作成されるのに対し、 runは各実行時に自動的に作成されることに注意してください。
LLM-finetuning
Please use V100 or A100!!!
💡
1. Data versioning (Artifacts) and visualize it (Table)
このハンズオンでは、アルパカデータセットを使用します。アルパカデータセットは、スタンフォード大学の研究者がOpenAIのダヴィンチモデルを使用して指示/出力のペアを生成し、ファインチューニングしたラマで開発した合成データセットです。このデータセットには、メール作成、ソーシャルメディア、生産性ツールを含む、多様なユーザー指向の指示が含まれています。
アルパカ-GPT4データセットは、単一のJSONファイル、alpaca_gpt4_data.jsonで構成されており、アルパカのプロンプトを使用してGPT-4によって生成された52Kの指示に従うデータが含まれています。このJSONファイルはアルパカデータと同じ形式をしていますが、出力はGPT-4によって生成されています。
An example:
instruction: str, describes the task the model should perform.Each of the 52K instructions is unique.input: str, optional context or input for the task.output: str, the answer to the instruction as generated by GPT-4.
W&Bテーブルとしてデータセットをログに記録しましょう。これにより、異なる指示/出力のペアを迅速に検査することができます。
Run set
59
可視化だけではなく、データはwandbのArtifactsを使って管理することができます。


# log to wandbwandb_project = "llm-finetuning-handson"with wandb.init(project=wandb_project):at = wandb.Artifact(name="alpaca_gpt4",type="dataset",description="A GPT4 generated Alpaca like dataset for instruction finetunning",metadata={"url":"https://github.com/Instruction-Tuning-with-GPT-4/GPT-4-LLM#how-good-is-the-data"},)at.add_file(dataset_file)wandb.log_artifact(at)# log as a tabletable = wandb.Table(columns=list(alpaca[0].keys()))for row in alpaca:table.add_data(*row.values())wandb.log({"alpaca_gpt4_table": table}

2. Training (Experiment tracking)
Let's fine-tune model! finetuningにおいては、HuggingFaceのintegrationを使うことができ、integrationを使用した場合、 run.log(loss)を毎回各必要がなくなります。
config = {"BASE_MODEL":"facebook/opt-125m","lora_config":{"r":32,"lora_alpha":16,'target_modules': [f"model.decoder.layers.{i}.self_attn.{proj}_proj" for i in range(31) for proj in ['q', 'k', 'v']],"lora_dropout":.1,"bias":"none","task_type":"CAUSAL_LM"},"training_args":{"dataloader_num_workers":16,"evaluation_strategy":"steps","per_device_train_batch_size":8,"max_steps": 50,"gradient_accumulation_steps":2,"report_to":"wandb",#wandb integration"warmup_steps":10,"num_train_epochs":1,"learning_rate":2e-4,"fp16":True,"logging_steps":10,"save_steps":10,"output_dir":'./outputs'}}with wandb.init(project=wandb_project, config=config, job_type="training") as run:# track datarun.use_artifact('wandb-public/llm-finetuning-handson/alpaca_gpt4_splitted:v0')# Setup for LoRalora_config = LoraConfig(**wandb.config["lora_config"])model_peft = get_peft_model(model, lora_config)model_peft.print_trainable_parameters()model_peft.config.use_cache = Falsetrainer = transformers.Trainer(model=model_peft,data_collator= collator,args=transformers.TrainingArguments(**wandb.config["training_args"]),train_dataset=train_dataset,eval_dataset=val_dataset)trainer.train()run.log_code()
3. Visualize pairs of input/output & Evaluation (Table)
ファインチューニングされたモデルと評価データセットを使用して、いくつかの推論を行いましょう。
このハンズオンでは取り上げませんが、下のレポートでモデルベースの評価方法について学ぶことができます。
Run: fluent-tree-9
1
Tableでは、テーブルデータだけではなく、画像や動画も含めることができます。

4. Hyper-parameter tuning (Sweep)
Sweepを使用すると、ハイパーパラメータの範囲内で複数のランを同時に実行し、どのハイパーパラメータのパターンが最高のパフォーマンスを生み出すかを理解するための優れた可視化を得ることができます。
Sweep: r8v60q8t 1
19
Sweep: r8v60q8t 2
0
5. Share results (Reports)

6. Manage models in the team and create automated process (Model registry / Automations / Launch)
Model registryを使用して、組織のモデルを管理することができます。Model registryから、AutomationとLaunchを使用して自動化プロセスを起動することもできます。Model registryについては、素敵なビデオとトレーニングで学ぶことができます。
Reference
A Gentle Introduction to LLM APIs
In this article, we dive into how large language models (LLMs) work, starting with tokenization and sampling, before exploring how to use them in your applications.
Prompt Engineering LLMs with LangChain and W&B
Join us for tips and tricks to improve your prompt engineering for LLMs. Then, stick around and find out how LangChain and W&B can make your life a whole lot easier.
How to Evaluate, Compare, and Optimize LLM Systems
This article provides an interactive look into how to go about evaluating your large language model (LLM) systems and how to approach optimizing the hyperparameters.
How to Evaluate an LLM, Part 1: Building an Evaluation Dataset for our LLM System
Building gold standard questions for evaluating our QA bot based on production data.
How to Fine-Tune an LLM Part 1: Preparing a Dataset for Instruction Tuning
Learn how to fine-tune an LLM on an instruction dataset! We'll cover how to format the data and train a model like Llama2, Mistral, etc. is this minimal example in (almost) pure PyTorch.
How to evaluate an LLM Part 3: LLMs evaluating LLMs
Employing auto-evaluation strategies to evaluate different component of our Wandbot RAG-based support system.
How to Fine-Tune an LLM Part 2: Instruction Tuning Llama 2
In part 1, we prepped our dataset. In part 2, we train our model

Training and Fine-tuning Large Language Models (LLMs)
Explore the architecture, training techniques, and fine-tuning methods for creating powerful LLMs. Gain theory and hands-on experience from Jonathan Frankle (MosaicML), and other industry leaders, and learn cutting-edge techniques like LoRA and RLHF.

Building LLM-Powered Apps
Learn how to build LLM-powered applications using LLM APIs, Langchain and W&B Prompts. This course will guide you through the entire process of designing, experimenting, and evaluating LLM-based apps.
Add a comment