Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions examples/train/notebook/llm/infer.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Inference\n",
"We have trained a well-trained checkpoint through the `self-cognition-sft.ipynb` tutorial, and here we use `PtEngine` to do the inference on it."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# import some libraries\n",
"import os\n",
"os.environ['CUDA_VISIBLE_DEVICES'] = '0'\n",
"\n",
"from swift.llm import InferEngine, InferRequest, PtEngine, RequestConfig, get_template\n",
"from swift.tuners import Swift"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# Hyperparameters for inference\n",
"last_model_checkpoint = '/mnt/nas2/huangjintao.hjt/work/llmscope/examples/train/notebook/output/Qwen2.5-3B-Instruct/checkpoint-93'\n",
"\n",
"# model\n",
"model = 'Qwen/Qwen2.5-3B-Instruct' # model_id or model_path\n",
"system = 'You are a helpful assistant.'\n",
"infer_backend = 'pt'\n",
"\n",
"# generation_config\n",
"max_new_tokens = 512\n",
"temperature = 0\n",
"stream = True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Get model and template, and load LoRA weights.\n",
"engine = PtEngine(model)\n",
"engine.model = Swift.from_pretrained(engine.model, last_model_checkpoint, inference_mode=True)\n",
"template = get_template(engine.model.model_meta.template, engine.tokenizer, default_system=system)\n",
"# The default mode of the template is 'pt', so there is no need to make any changes.\n",
"# template.set_mode('pt')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"query_list = [\n",
" 'who are you?',\n",
" \"What should I do if I can't sleep at night?\",\n",
" '你是谁训练的?',\n",
"]\n",
"\n",
"def infer_stream(engine: InferEngine, infer_request: InferRequest):\n",
" request_config = RequestConfig(max_tokens=max_new_tokens, temperature=temperature, stream=True)\n",
" gen = engine.infer([infer_request], request_config)\n",
" query = infer_request.messages[0]['content']\n",
" print(f'query: {query}\\nresponse: ', end='')\n",
" for resp_list in gen:\n",
" print(resp_list[0].choices[0].delta.content, end='', flush=True)\n",
" print()\n",
"\n",
"def infer(engine: InferEngine, infer_request: InferRequest):\n",
" request_config = RequestConfig(max_tokens=max_new_tokens, temperature=temperature)\n",
" resp_list = engine.infer([infer_request], request_config)\n",
" query = infer_request.messages[0]['content']\n",
" response = resp_list[0].choices[0].message.content\n",
" print(f'query: {query}')\n",
" print(f'response: {response}')\n",
"\n",
"\n",
"infer_func = infer_stream if stream else infer\n",
"for query in query_list:\n",
" infer_func(engine, InferRequest(messages=[{'role': 'user', 'content': query}]))\n",
" print('-' * 50)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "test_py310",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.15"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
222 changes: 222 additions & 0 deletions examples/train/notebook/llm/self-cognition-sft.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 10-minute self-cognition fine-tuning\n",
"\n",
"Here is a demonstration of using python to perform self-cognition fine-tuning of Qwen2-7B-Instruct. Through this tutorial, you can quickly understand some details of swift sft, which will be of great help in customizing ms-swift for you~\n",
"\n",
"Are you ready? Let's begin the journey..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "shellscript"
}
},
"outputs": [],
"source": [
"# install ms-swift\n",
"!git clone https://github.com/modelscope/ms-swift.git\n",
"%cd ms-swift\n",
"!pip install -e '.[llm]'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# import some libraries\n",
"import os\n",
"os.environ['CUDA_VISIBLE_DEVICES'] = '0'\n",
"\n",
"from swift.llm import get_model_tokenizer, load_dataset, get_template, EncodePreprocessor\n",
"from swift.utils import get_logger, find_all_linears, get_model_parameter_info, plot_images, seed_everything\n",
"from swift.tuners import Swift, LoraConfig\n",
"\n",
"from swift.trainers import Seq2SeqTrainer, Seq2SeqTrainingArguments\n",
"\n",
"from functools import partial\n",
"\n",
"\n",
"logger = get_logger()\n",
"seed_everything(42)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Hyperparameters for training\n",
"# model\n",
"model = 'Qwen/Qwen2.5-3B-Instruct' # model_id or model_path\n",
"system = 'You are a helpful assistant.'\n",
"output_dir = 'output/Qwen2.5-3B-Instruct'\n",
"\n",
"# dataset\n",
"dataset = ['AI-ModelScope/alpaca-gpt4-data-zh#500', 'AI-ModelScope/alpaca-gpt4-data-en#500',\n",
" 'swift/self-cognition#500'] # dataset_id or dataset_path\n",
"max_length = 2048\n",
"split_dataset_ratio = 0.01 # Split validation set\n",
"num_proc = 4 # The number of processes for data loading.\n",
"# The following two parameters are used to override the placeholders in the self-cognition dataset.\n",
"model_name = ['小黄', 'Xiao Huang'] # The Chinese name and English name of the model\n",
"model_author = ['魔搭', 'ModelScope'] # The Chinese name and English name of the model author\n",
"\n",
"# lora\n",
"lora_rank = 8\n",
"lora_alpha = 32\n",
"\n",
"# training_args\n",
"training_args = Seq2SeqTrainingArguments(\n",
" output_dir=output_dir,\n",
" learning_rate=1e-4,\n",
" per_device_train_batch_size=1,\n",
" per_device_eval_batch_size=1,\n",
" gradient_checkpointing=True,\n",
" weight_decay=0.1,\n",
" lr_scheduler_type='cosine',\n",
" warmup_ratio=0.05,\n",
" report_to=['tensorboard'],\n",
" logging_first_step=True,\n",
" save_strategy='steps',\n",
" save_steps=50,\n",
" eval_strategy='steps',\n",
" eval_steps=50,\n",
" gradient_accumulation_steps=16,\n",
" num_train_epochs=1,\n",
" metric_for_best_model='loss',\n",
" save_total_limit=2,\n",
" logging_steps=5,\n",
" dataloader_num_workers=1,\n",
")\n",
"\n",
"\n",
"output_dir = os.path.abspath(os.path.expanduser(output_dir))\n",
"logger.info(f'output_dir: {output_dir}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Obtain the model and template, and add a trainable Lora layer on the model.\n",
"model, tokenizer = get_model_tokenizer(model)\n",
"logger.info(f'model_info: {model.model_info}')\n",
"template = get_template(model.model_meta.template, tokenizer, default_system=system, max_length=max_length)\n",
"template.set_mode('train')\n",
"\n",
"target_modules = find_all_linears(model)\n",
"lora_config = LoraConfig(task_type='CAUSAL_LM', r=lora_rank, lora_alpha=lora_alpha,\n",
" target_modules=target_modules)\n",
"model = Swift.prepare_model(model, lora_config)\n",
"logger.info(f'lora_config: {lora_config}')\n",
"\n",
"# Print model structure and trainable parameters.\n",
"logger.info(f'model: {model}')\n",
"model_parameter_info = get_model_parameter_info(model)\n",
"logger.info(f'model_parameter_info: {model_parameter_info}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Download and load the dataset, split it into a training set and a validation set,\n",
"# and encode the text data into tokens.\n",
"train_dataset, val_dataset = load_dataset(dataset, split_dataset_ratio=split_dataset_ratio, num_proc=num_proc,\n",
" model_name=model_name, model_author=model_author)\n",
"\n",
"logger.info(f'train_dataset: {train_dataset}')\n",
"logger.info(f'val_dataset: {val_dataset}')\n",
"logger.info(f'train_dataset[0]: {train_dataset[0]}')\n",
"\n",
"train_dataset = EncodePreprocessor(template=template)(train_dataset, num_proc=num_proc)\n",
"val_dataset = EncodePreprocessor(template=template)(val_dataset, num_proc=num_proc)\n",
"logger.info(f'encoded_train_dataset[0]: {train_dataset[0]}')\n",
"\n",
"# Print a sample\n",
"template.print_inputs(train_dataset[0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Get the trainer and start the training.\n",
"model.enable_input_require_grads() # Compatible with gradient checkpointing\n",
"trainer = Seq2SeqTrainer(\n",
" model=model,\n",
" args=training_args,\n",
" data_collator=template.data_collator,\n",
" train_dataset=train_dataset,\n",
" eval_dataset=val_dataset,\n",
" template=template,\n",
")\n",
"trainer.model_accepts_loss_kwargs = True # Compatible with transformers >= 4.46\n",
"trainer.train()\n",
"\n",
"last_model_checkpoint = trainer.state.last_model_checkpoint\n",
"logger.info(f'last_model_checkpoint: {last_model_checkpoint}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Visualize the training loss.\n",
"# You can also use the TensorBoard visualization interface during training by entering\n",
"# `tensorboard --logdir '{output_dir}/runs'` at the command line.\n",
"images_dir = os.path.join(output_dir, 'images')\n",
"logger.info(f'images_dir: {images_dir}')\n",
"plot_images(images_dir, training_args.logging_dir, ['train/loss'], 0.9) # save images\n",
"\n",
"# Read and display the image.\n",
"# The light yellow line represents the actual loss value,\n",
"# while the yellow line represents the loss value smoothed with a smoothing factor of 0.9.\n",
"from IPython.display import display\n",
"from PIL import Image\n",
"image = Image.open(os.path.join(images_dir, 'train_loss.png'))\n",
"display(image)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "py310",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.15"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading