'}]}"
- ]
- },
- "execution_count": 9,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "dataset[5]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [],
- "source": [
- "training_args = TrainingArguments(\n",
- " output_dir=str(args.output_dir),\n",
- " num_train_epochs=3,\n",
- " per_device_train_batch_size=1,\n",
- " gradient_accumulation_steps=16,\n",
- " gradient_checkpointing=True,\n",
- " learning_rate=2e-5,\n",
- " lr_scheduler_type=\"cosine\",\n",
- " warmup_ratio=0.05,\n",
- " weight_decay=0.01,\n",
- " fp16=not is_bfloat16_supported(),\n",
- " bf16=is_bfloat16_supported(),\n",
- " logging_steps=10,\n",
- " save_steps=200,\n",
- " save_total_limit=3,\n",
- " report_to=\"none\",\n",
- " optim=\"adamw_8bit\",\n",
- ")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "num_proc must be <= 20. Reducing num_proc to 20 for dataset of size 20.\n"
- ]
- }
- ],
- "source": [
- "trainer = SFTTrainer(\n",
- " model=model,\n",
- " tokenizer=tokenizer,\n",
- " args=training_args,\n",
- " train_dataset=dataset.to_transformers_dataset(tokenizer),\n",
- " dataset_text_field=\"messages\",\n",
- " max_seq_length=max_seq_length,\n",
- ")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {},
- "outputs": [
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "==((====))== Unsloth - 2x faster free finetuning | Num GPUs used = 1\n",
- " \\\\ /| Num examples = 20 | Num Epochs = 3 | Total steps = 3\n",
- "O^O/ \\_/ \\ Batch size per device = 1 | Gradient accumulation steps = 16\n",
- "\\ / Data Parallel GPUs = 1 | Total batch size (1 x 16 x 1) = 16\n",
- " \"-____-\" Trainable parameters = 536,870,912/32,000,000,000 (1.68% trained)\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Unsloth: Will smartly offload gradients to save VRAM!\n"
- ]
- },
- {
- "data": {
- "text/html": [
- "\n",
- " \n",
- " \n",
- "
\n",
- " [3/3 01:45, Epoch 1/3]\n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " | Step | \n",
- " Training Loss | \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- "
"
- ],
- "text/plain": [
- ""
- ]
- },
- "metadata": {},
- "output_type": "display_data"
- },
- {
- "data": {
- "text/plain": [
- "TrainOutput(global_step=3, training_loss=1.7514243125915527, metrics={'train_runtime': 187.9691, 'train_samples_per_second': 0.319, 'train_steps_per_second': 0.016, 'total_flos': 7927521441988608.0, 'train_loss': 1.7514243125915527})"
- ]
- },
- "execution_count": 12,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "trainer.train()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Unsloth: Merging 4bit and LoRA weights to 16bit...\n",
- "Unsloth: Will use up to 312.94 out of 503.54 RAM for saving.\n",
- "Unsloth: Saving model... This might take 5 minutes ...\n"
- ]
- },
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- " 12%|█▎ | 8/64 [00:00<00:02, 22.74it/s]\n",
- "We will save to Disk and not RAM now.\n",
- "100%|██████████| 64/64 [00:43<00:00, 1.48it/s]\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Unsloth: Saving tokenizer... Done.\n",
- "Done.\n"
- ]
- }
- ],
- "source": [
- "model.save_pretrained_merged(\n",
- " str(args.output_dir), \n",
- " tokenizer=tokenizer,\n",
- " #save_method=\"merged_4bit_forced\"\n",
- ")"
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "train",
- "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.12"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
-}