diff --git a/learn/vlm-finetuning/.gitignore b/learn/vlm-finetuning/.gitignore new file mode 100644 index 0000000..8420f2d --- /dev/null +++ b/learn/vlm-finetuning/.gitignore @@ -0,0 +1 @@ +qwen2p5-vl-7b-instruct/ \ No newline at end of file diff --git a/learn/vlm-finetuning/finetuning-on-bedrock.ipynb b/learn/vlm-finetuning/finetuning-on-bedrock.ipynb new file mode 100644 index 0000000..17ca069 --- /dev/null +++ b/learn/vlm-finetuning/finetuning-on-bedrock.ipynb @@ -0,0 +1,1480 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# How to Finetune a VLM on Bedrock and deploy on Fireworks\n", + "\n", + "This guide will use Qwen 2.5 VL 7B, but you can use this guide with any other model.\n", + "\n", + "### Pre-requisites\n", + "- [aws-cli](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)\n", + "- Python" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Collecting boto3\n", + " Using cached boto3-1.38.14-py3-none-any.whl.metadata (6.6 kB)\n", + "Collecting huggingface-hub[cli]\n", + " Using cached huggingface_hub-0.31.1-py3-none-any.whl.metadata (13 kB)\n", + "Collecting botocore<1.39.0,>=1.38.14 (from boto3)\n", + " Using cached botocore-1.38.14-py3-none-any.whl.metadata (5.7 kB)\n", + "Collecting jmespath<2.0.0,>=0.7.1 (from boto3)\n", + " Using cached jmespath-1.0.1-py3-none-any.whl.metadata (7.6 kB)\n", + "Collecting s3transfer<0.13.0,>=0.12.0 (from boto3)\n", + " Using cached s3transfer-0.12.0-py3-none-any.whl.metadata (1.7 kB)\n", + "Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in /Users/aidando/miniconda3/envs/bedrock-env-2/lib/python3.10/site-packages (from botocore<1.39.0,>=1.38.14->boto3) (2.9.0.post0)\n", + "Collecting urllib3!=2.2.0,<3,>=1.25.4 (from botocore<1.39.0,>=1.38.14->boto3)\n", + " Using cached urllib3-2.4.0-py3-none-any.whl.metadata (6.5 kB)\n", + "Requirement already satisfied: six>=1.5 in /Users/aidando/miniconda3/envs/bedrock-env-2/lib/python3.10/site-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.39.0,>=1.38.14->boto3) (1.17.0)\n", + "Collecting filelock (from huggingface-hub[cli])\n", + " Using cached filelock-3.18.0-py3-none-any.whl.metadata (2.9 kB)\n", + "Collecting fsspec>=2023.5.0 (from huggingface-hub[cli])\n", + " Using cached fsspec-2025.3.2-py3-none-any.whl.metadata (11 kB)\n", + "Requirement already satisfied: packaging>=20.9 in /Users/aidando/miniconda3/envs/bedrock-env-2/lib/python3.10/site-packages (from huggingface-hub[cli]) (25.0)\n", + "Collecting pyyaml>=5.1 (from huggingface-hub[cli])\n", + " Using cached PyYAML-6.0.2-cp310-cp310-macosx_11_0_arm64.whl.metadata (2.1 kB)\n", + "Collecting requests (from huggingface-hub[cli])\n", + " Using cached requests-2.32.3-py3-none-any.whl.metadata (4.6 kB)\n", + "Collecting tqdm>=4.42.1 (from huggingface-hub[cli])\n", + " Using cached tqdm-4.67.1-py3-none-any.whl.metadata (57 kB)\n", + "Requirement already satisfied: typing-extensions>=3.7.4.3 in /Users/aidando/miniconda3/envs/bedrock-env-2/lib/python3.10/site-packages (from huggingface-hub[cli]) (4.13.2)\n", + "Collecting hf-xet<2.0.0,>=1.1.0 (from huggingface-hub[cli])\n", + " Downloading hf_xet-1.1.1-cp37-abi3-macosx_11_0_arm64.whl.metadata (494 bytes)\n", + "Collecting InquirerPy==0.3.4 (from huggingface-hub[cli])\n", + " Using cached InquirerPy-0.3.4-py3-none-any.whl.metadata (8.1 kB)\n", + "Collecting pfzy<0.4.0,>=0.3.1 (from InquirerPy==0.3.4->huggingface-hub[cli])\n", + " Using cached pfzy-0.3.4-py3-none-any.whl.metadata (4.9 kB)\n", + "Requirement already satisfied: prompt-toolkit<4.0.0,>=3.0.1 in /Users/aidando/miniconda3/envs/bedrock-env-2/lib/python3.10/site-packages (from InquirerPy==0.3.4->huggingface-hub[cli]) (3.0.51)\n", + "Requirement already satisfied: wcwidth in /Users/aidando/miniconda3/envs/bedrock-env-2/lib/python3.10/site-packages (from prompt-toolkit<4.0.0,>=3.0.1->InquirerPy==0.3.4->huggingface-hub[cli]) (0.2.13)\n", + "Collecting charset-normalizer<4,>=2 (from requests->huggingface-hub[cli])\n", + " Using cached charset_normalizer-3.4.2-cp310-cp310-macosx_10_9_universal2.whl.metadata (35 kB)\n", + "Collecting idna<4,>=2.5 (from requests->huggingface-hub[cli])\n", + " Using cached idna-3.10-py3-none-any.whl.metadata (10 kB)\n", + "Collecting certifi>=2017.4.17 (from requests->huggingface-hub[cli])\n", + " Using cached certifi-2025.4.26-py3-none-any.whl.metadata (2.5 kB)\n", + "Using cached boto3-1.38.14-py3-none-any.whl (139 kB)\n", + "Using cached botocore-1.38.14-py3-none-any.whl (13.6 MB)\n", + "Using cached jmespath-1.0.1-py3-none-any.whl (20 kB)\n", + "Using cached s3transfer-0.12.0-py3-none-any.whl (84 kB)\n", + "Using cached urllib3-2.4.0-py3-none-any.whl (128 kB)\n", + "Using cached huggingface_hub-0.31.1-py3-none-any.whl (484 kB)\n", + "Using cached InquirerPy-0.3.4-py3-none-any.whl (67 kB)\n", + "Downloading hf_xet-1.1.1-cp37-abi3-macosx_11_0_arm64.whl (5.1 MB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m5.1/5.1 MB\u001b[0m \u001b[31m27.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n", + "\u001b[?25hUsing cached pfzy-0.3.4-py3-none-any.whl (8.5 kB)\n", + "Using cached fsspec-2025.3.2-py3-none-any.whl (194 kB)\n", + "Using cached PyYAML-6.0.2-cp310-cp310-macosx_11_0_arm64.whl (171 kB)\n", + "Using cached tqdm-4.67.1-py3-none-any.whl (78 kB)\n", + "Using cached filelock-3.18.0-py3-none-any.whl (16 kB)\n", + "Using cached requests-2.32.3-py3-none-any.whl (64 kB)\n", + "Using cached charset_normalizer-3.4.2-cp310-cp310-macosx_10_9_universal2.whl (201 kB)\n", + "Using cached idna-3.10-py3-none-any.whl (70 kB)\n", + "Using cached certifi-2025.4.26-py3-none-any.whl (159 kB)\n", + "Installing collected packages: urllib3, tqdm, pyyaml, pfzy, jmespath, idna, hf-xet, fsspec, filelock, charset-normalizer, certifi, requests, InquirerPy, botocore, s3transfer, huggingface-hub, boto3\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m17/17\u001b[0m [boto3]m16/17\u001b[0m [boto3]re]\n", + "\u001b[1A\u001b[2KSuccessfully installed InquirerPy-0.3.4 boto3-1.38.14 botocore-1.38.14 certifi-2025.4.26 charset-normalizer-3.4.2 filelock-3.18.0 fsspec-2025.3.2 hf-xet-1.1.1 huggingface-hub-0.31.1 idna-3.10 jmespath-1.0.1 pfzy-0.3.4 pyyaml-6.0.2 requests-2.32.3 s3transfer-0.12.0 tqdm-4.67.1 urllib3-2.4.0\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], + "source": [ + "%pip install boto3 \"huggingface-hub[cli]\"" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "BUCKET_NAME fw-bedrock-finetuning-023979239971\n", + "HF_MODEL_NAME Qwen/Qwen2.5-VL-7B-Instruct\n", + "MODEL_NAME qwen2p5-vl-7b-instruct\n" + ] + } + ], + "source": [ + "import boto3\n", + "\n", + "account_id = boto3.client('sts').get_caller_identity()['Account']\n", + "\n", + "BUCKET_NAME=f\"fw-bedrock-finetuning-{account_id}\"\n", + "HF_MODEL_NAME=\"Qwen/Qwen2.5-VL-7B-Instruct\"\n", + "MODEL_NAME=HF_MODEL_NAME.lower().replace(\".\", \"p\").split(\"/\")[-1]\n", + "\n", + "print(\"BUCKET_NAME\", BUCKET_NAME)\n", + "print(\"HF_MODEL_NAME\", HF_MODEL_NAME)\n", + "print(\"MODEL_NAME\", MODEL_NAME)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Created bucket: fw-bedrock-finetuning-023979239971\n" + ] + } + ], + "source": [ + "s3 = boto3.client('s3')\n", + "\n", + "try:\n", + " s3.create_bucket(Bucket=BUCKET_NAME)\n", + " print(f\"Created bucket: {BUCKET_NAME}\")\n", + "except s3.exceptions.BucketAlreadyExists:\n", + " print(f\"Bucket {BUCKET_NAME} already exists\")\n", + "except s3.exceptions.BucketAlreadyOwnedByYou:\n", + " print(f\"Bucket {BUCKET_NAME} already owned by you\")" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Fetching 16 files: 0%| | 0/16 [00:00