Datasets:
Tags:
Not-For-All-Audiences
Upload lora_forward_pass_test.ipynb
Browse files- lora_forward_pass_test.ipynb +73 -101
lora_forward_pass_test.ipynb
CHANGED
|
@@ -2466,13 +2466,10 @@
|
|
| 2466 |
],
|
| 2467 |
"source": [
|
| 2468 |
"#@markdown # CELL 1 — Setup environment + login\n",
|
| 2469 |
-
"\n",
|
| 2470 |
"from google.colab import drive, userdata\n",
|
| 2471 |
"from huggingface_hub import login\n",
|
| 2472 |
"import torch, os, gc\n",
|
| 2473 |
-
"\n",
|
| 2474 |
"drive.mount(\"/content/drive\")\n",
|
| 2475 |
-
"\n",
|
| 2476 |
"hf_token = userdata.get(\"HF_TOKEN\")\n",
|
| 2477 |
"if hf_token:\n",
|
| 2478 |
" login(token=hf_token)\n",
|
|
@@ -2485,7 +2482,6 @@
|
|
| 2485 |
"\n",
|
| 2486 |
"import torchao\n",
|
| 2487 |
"print(f\"✅ torchao updated to version: {torchao.__version__}\")\n",
|
| 2488 |
-
"\n",
|
| 2489 |
"!pip install -q safetensors huggingface_hub diffusers transformers accelerate sdnq\n",
|
| 2490 |
"\n",
|
| 2491 |
"#images for testing\n",
|
|
@@ -2498,9 +2494,7 @@
|
|
| 2498 |
"prompt1= 'a cute dog on grass'\n",
|
| 2499 |
"prompt2 = 'a golden retriever on a road'\n",
|
| 2500 |
"#-----#\n",
|
| 2501 |
-
"\n",
|
| 2502 |
"print(\"✅ Dependencies installed\")\n",
|
| 2503 |
-
"\n",
|
| 2504 |
"print(\"✅ Logged into Hugging Face\")\n",
|
| 2505 |
"\n",
|
| 2506 |
"import gc\n",
|
|
@@ -2518,11 +2512,9 @@
|
|
| 2518 |
"from huggingface_hub import hf_hub_download\n",
|
| 2519 |
"import hashlib\n",
|
| 2520 |
"from pathlib import Path\n",
|
| 2521 |
-
"\n",
|
| 2522 |
"import torch\n",
|
| 2523 |
"from transformers import BitsAndBytesConfig\n",
|
| 2524 |
"\n",
|
| 2525 |
-
"\n",
|
| 2526 |
"# Patch function\n",
|
| 2527 |
"def patch_encode_prompt(pipe, cached_prompt_embeds, cached_text_ids, gpu_id):\n",
|
| 2528 |
" def patched(self, prompt=None, device=None, num_images_per_prompt=1, prompt_embeds=None, **kwargs):\n",
|
|
@@ -2540,15 +2532,12 @@
|
|
| 2540 |
" pipe.encode_prompt = partial(patched, pipe)\n",
|
| 2541 |
" return pipe\n",
|
| 2542 |
"\n",
|
| 2543 |
-
"\n",
|
| 2544 |
"# Load single pipe\n",
|
| 2545 |
"def load_klein_pipe(gpu_id: int, pipe_id: int):\n",
|
| 2546 |
" torch.cuda.set_device(gpu_id)\n",
|
| 2547 |
-
"\n",
|
| 2548 |
" print(f\" [GPU {gpu_id} - Pipe {pipe_id}] Loading...\")\n",
|
| 2549 |
-
"\n",
|
| 2550 |
" MODEL_ID = 'codeShare/Flux.2-Klein-4B-SDNQ-4bit'\n",
|
| 2551 |
-
"\n",
|
| 2552 |
" edit_prompt = 'this is a test'\n",
|
| 2553 |
" pipe = Flux2KleinPipeline.from_pretrained(\n",
|
| 2554 |
" MODEL_ID,\n",
|
|
@@ -2575,7 +2564,6 @@
|
|
| 2575 |
" #-----#\n",
|
| 2576 |
" gc.collect()\n",
|
| 2577 |
" torch.cuda.empty_cache()\n",
|
| 2578 |
-
"\n",
|
| 2579 |
" pipe.transformer.to(f\"cuda:{gpu_id}\")\n",
|
| 2580 |
" # pipe.enable_model_cpu_offload(gpu_id=gpu_id) # Removed: This can interfere with VAE device placement during training\n",
|
| 2581 |
"\n",
|
|
@@ -2584,7 +2572,6 @@
|
|
| 2584 |
" #pipe.vae.enable_slicing()\n",
|
| 2585 |
" #pipe.vae.enable_tiling()\n",
|
| 2586 |
" #pipe.enable_attention_slicing(1)\n",
|
| 2587 |
-
"\n",
|
| 2588 |
" print(f\" [GPU {gpu_id} - Pipe {pipe_id}] Ready | VRAM: {torch.cuda.memory_allocated(gpu_id)/1e9:.2f} GB\")\n",
|
| 2589 |
" return pipe\n",
|
| 2590 |
"#----#\n"
|
|
@@ -2596,10 +2583,35 @@
|
|
| 2596 |
"gc.collect()\n",
|
| 2597 |
"torch.cuda.empty_cache()\n",
|
| 2598 |
"NUM_GPUS = torch.cuda.device_count()\n",
|
| 2599 |
-
"
|
| 2600 |
-
"
|
| 2601 |
-
"
|
| 2602 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2603 |
],
|
| 2604 |
"metadata": {
|
| 2605 |
"colab": {
|
|
@@ -2762,81 +2774,6 @@
|
|
| 2762 |
}
|
| 2763 |
]
|
| 2764 |
},
|
| 2765 |
-
{
|
| 2766 |
-
"cell_type": "code",
|
| 2767 |
-
"source": [
|
| 2768 |
-
"pipe.transformer = pipe.transformer.to('cuda')\n",
|
| 2769 |
-
"gc.collect()\n",
|
| 2770 |
-
"torch.cuda.empty_cache()\n",
|
| 2771 |
-
"from peft import prepare_model_for_kbit_training\n",
|
| 2772 |
-
"pipe.transformer = prepare_model_for_kbit_training(pipe.transformer)"
|
| 2773 |
-
],
|
| 2774 |
-
"metadata": {
|
| 2775 |
-
"id": "sEhh282dy1Ls"
|
| 2776 |
-
},
|
| 2777 |
-
"execution_count": 3,
|
| 2778 |
-
"outputs": []
|
| 2779 |
-
},
|
| 2780 |
-
{
|
| 2781 |
-
"cell_type": "code",
|
| 2782 |
-
"source": [
|
| 2783 |
-
"from peft import LoraConfig\n",
|
| 2784 |
-
"\n",
|
| 2785 |
-
"config = LoraConfig(\n",
|
| 2786 |
-
" r=16,\n",
|
| 2787 |
-
" lora_alpha=8,\n",
|
| 2788 |
-
" target_modules=[\n",
|
| 2789 |
-
" \"attn.to_q\", \"attn.to_k\", \"attn.to_v\",\n",
|
| 2790 |
-
" \"ff.linear_in\", \"ff.linear_out\",\n",
|
| 2791 |
-
" \"ff_context.linear_in\", \"ff_context.linear_out\",\n",
|
| 2792 |
-
" \"attn.to_qkv_mlp_proj\",\n",
|
| 2793 |
-
"],\n",
|
| 2794 |
-
" lora_dropout=0.05,\n",
|
| 2795 |
-
" bias=\"none\",\n",
|
| 2796 |
-
")"
|
| 2797 |
-
],
|
| 2798 |
-
"metadata": {
|
| 2799 |
-
"id": "7w3yiYzhuo0A"
|
| 2800 |
-
},
|
| 2801 |
-
"execution_count": 4,
|
| 2802 |
-
"outputs": []
|
| 2803 |
-
},
|
| 2804 |
-
{
|
| 2805 |
-
"cell_type": "code",
|
| 2806 |
-
"source": [
|
| 2807 |
-
"from peft import get_peft_model\n",
|
| 2808 |
-
"\n",
|
| 2809 |
-
"pipe.transformer = get_peft_model(pipe.transformer, config)"
|
| 2810 |
-
],
|
| 2811 |
-
"metadata": {
|
| 2812 |
-
"id": "qTSuLZU7uzTk"
|
| 2813 |
-
},
|
| 2814 |
-
"execution_count": 5,
|
| 2815 |
-
"outputs": []
|
| 2816 |
-
},
|
| 2817 |
-
{
|
| 2818 |
-
"cell_type": "code",
|
| 2819 |
-
"source": [
|
| 2820 |
-
"pipe.transformer.print_trainable_parameters()"
|
| 2821 |
-
],
|
| 2822 |
-
"metadata": {
|
| 2823 |
-
"colab": {
|
| 2824 |
-
"base_uri": "https://localhost:8080/"
|
| 2825 |
-
},
|
| 2826 |
-
"id": "_6k8-Z0twPn-",
|
| 2827 |
-
"outputId": "3f3cc893-1e91-4b77-8121-7a741ae177dd"
|
| 2828 |
-
},
|
| 2829 |
-
"execution_count": 6,
|
| 2830 |
-
"outputs": [
|
| 2831 |
-
{
|
| 2832 |
-
"output_type": "stream",
|
| 2833 |
-
"name": "stdout",
|
| 2834 |
-
"text": [
|
| 2835 |
-
"trainable params: 16,711,680 || all params: 2,167,021,056 || trainable%: 0.7712\n"
|
| 2836 |
-
]
|
| 2837 |
-
}
|
| 2838 |
-
]
|
| 2839 |
-
},
|
| 2840 |
{
|
| 2841 |
"cell_type": "code",
|
| 2842 |
"source": [
|
|
@@ -2875,7 +2812,17 @@
|
|
| 2875 |
"\n",
|
| 2876 |
" def __getitem__(self, idx):\n",
|
| 2877 |
" image = self.transform(self.images[idx])\n",
|
| 2878 |
-
" return image, self.prompts[idx]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2879 |
"\n",
|
| 2880 |
"dataset = Flux2KleinDataset(image_paths, instance_prompts, resolution=512)\n",
|
| 2881 |
"dataloader = DataLoader(dataset, batch_size=1, shuffle=True)\n",
|
|
@@ -2883,14 +2830,39 @@
|
|
| 2883 |
"# ====================== TRAINING SETUP ======================\n",
|
| 2884 |
"device = pipe.device\n",
|
| 2885 |
"dtype = torch.float16 # or bfloat16 if supported\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2886 |
"\n",
|
| 2887 |
-
"
|
| 2888 |
-
"pipe.transformer.train()\n",
|
| 2889 |
-
"\n",
|
| 2890 |
-
"# Optional: Enable gradient checkpointing for memory savings\n",
|
| 2891 |
-
"#pipe.transformer.gradient_checkpointing_enable()\n",
|
| 2892 |
"\n",
|
| 2893 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2894 |
"num_epochs = 5\n",
|
| 2895 |
"accumulation_steps = 2\n",
|
| 2896 |
"\n",
|
|
@@ -2908,7 +2880,7 @@
|
|
| 2908 |
" with torch.no_grad():\n",
|
| 2909 |
" # FLUX VAE expects [-1, 1]\n",
|
| 2910 |
" latents = pipe.vae.encode(image * 2 - 1).latent_dist.sample()\n",
|
| 2911 |
-
" # No scaling factor
|
| 2912 |
"\n",
|
| 2913 |
" # ====================== TEXT ENCODING ======================\n",
|
| 2914 |
" with torch.no_grad():\n",
|
|
|
|
| 2466 |
],
|
| 2467 |
"source": [
|
| 2468 |
"#@markdown # CELL 1 — Setup environment + login\n",
|
|
|
|
| 2469 |
"from google.colab import drive, userdata\n",
|
| 2470 |
"from huggingface_hub import login\n",
|
| 2471 |
"import torch, os, gc\n",
|
|
|
|
| 2472 |
"drive.mount(\"/content/drive\")\n",
|
|
|
|
| 2473 |
"hf_token = userdata.get(\"HF_TOKEN\")\n",
|
| 2474 |
"if hf_token:\n",
|
| 2475 |
" login(token=hf_token)\n",
|
|
|
|
| 2482 |
"\n",
|
| 2483 |
"import torchao\n",
|
| 2484 |
"print(f\"✅ torchao updated to version: {torchao.__version__}\")\n",
|
|
|
|
| 2485 |
"!pip install -q safetensors huggingface_hub diffusers transformers accelerate sdnq\n",
|
| 2486 |
"\n",
|
| 2487 |
"#images for testing\n",
|
|
|
|
| 2494 |
"prompt1= 'a cute dog on grass'\n",
|
| 2495 |
"prompt2 = 'a golden retriever on a road'\n",
|
| 2496 |
"#-----#\n",
|
|
|
|
| 2497 |
"print(\"✅ Dependencies installed\")\n",
|
|
|
|
| 2498 |
"print(\"✅ Logged into Hugging Face\")\n",
|
| 2499 |
"\n",
|
| 2500 |
"import gc\n",
|
|
|
|
| 2512 |
"from huggingface_hub import hf_hub_download\n",
|
| 2513 |
"import hashlib\n",
|
| 2514 |
"from pathlib import Path\n",
|
|
|
|
| 2515 |
"import torch\n",
|
| 2516 |
"from transformers import BitsAndBytesConfig\n",
|
| 2517 |
"\n",
|
|
|
|
| 2518 |
"# Patch function\n",
|
| 2519 |
"def patch_encode_prompt(pipe, cached_prompt_embeds, cached_text_ids, gpu_id):\n",
|
| 2520 |
" def patched(self, prompt=None, device=None, num_images_per_prompt=1, prompt_embeds=None, **kwargs):\n",
|
|
|
|
| 2532 |
" pipe.encode_prompt = partial(patched, pipe)\n",
|
| 2533 |
" return pipe\n",
|
| 2534 |
"\n",
|
|
|
|
| 2535 |
"# Load single pipe\n",
|
| 2536 |
"def load_klein_pipe(gpu_id: int, pipe_id: int):\n",
|
| 2537 |
" torch.cuda.set_device(gpu_id)\n",
|
|
|
|
| 2538 |
" print(f\" [GPU {gpu_id} - Pipe {pipe_id}] Loading...\")\n",
|
|
|
|
| 2539 |
" MODEL_ID = 'codeShare/Flux.2-Klein-4B-SDNQ-4bit'\n",
|
| 2540 |
+
" #----#\n",
|
| 2541 |
" edit_prompt = 'this is a test'\n",
|
| 2542 |
" pipe = Flux2KleinPipeline.from_pretrained(\n",
|
| 2543 |
" MODEL_ID,\n",
|
|
|
|
| 2564 |
" #-----#\n",
|
| 2565 |
" gc.collect()\n",
|
| 2566 |
" torch.cuda.empty_cache()\n",
|
|
|
|
| 2567 |
" pipe.transformer.to(f\"cuda:{gpu_id}\")\n",
|
| 2568 |
" # pipe.enable_model_cpu_offload(gpu_id=gpu_id) # Removed: This can interfere with VAE device placement during training\n",
|
| 2569 |
"\n",
|
|
|
|
| 2572 |
" #pipe.vae.enable_slicing()\n",
|
| 2573 |
" #pipe.vae.enable_tiling()\n",
|
| 2574 |
" #pipe.enable_attention_slicing(1)\n",
|
|
|
|
| 2575 |
" print(f\" [GPU {gpu_id} - Pipe {pipe_id}] Ready | VRAM: {torch.cuda.memory_allocated(gpu_id)/1e9:.2f} GB\")\n",
|
| 2576 |
" return pipe\n",
|
| 2577 |
"#----#\n"
|
|
|
|
| 2583 |
"gc.collect()\n",
|
| 2584 |
"torch.cuda.empty_cache()\n",
|
| 2585 |
"NUM_GPUS = torch.cuda.device_count()\n",
|
| 2586 |
+
"#----#\n",
|
| 2587 |
+
"PIPE_ID = 1\n",
|
| 2588 |
+
"GPU_ID = 0\n",
|
| 2589 |
+
"pipe = load_klein_pipe(GPU_ID , PIPE_ID)\n",
|
| 2590 |
+
"print(f'loaded pipe to {pipe.device}')\n",
|
| 2591 |
+
"#----#\n",
|
| 2592 |
+
"pipe.transformer = pipe.transformer.to('cuda')\n",
|
| 2593 |
+
"gc.collect()\n",
|
| 2594 |
+
"torch.cuda.empty_cache()\n",
|
| 2595 |
+
"from peft import prepare_model_for_kbit_training\n",
|
| 2596 |
+
"pipe.transformer = prepare_model_for_kbit_training(pipe.transformer)\n",
|
| 2597 |
+
"#----#\n",
|
| 2598 |
+
"from peft import LoraConfig\n",
|
| 2599 |
+
"config = LoraConfig(\n",
|
| 2600 |
+
" r=16,\n",
|
| 2601 |
+
" lora_alpha=8,\n",
|
| 2602 |
+
" target_modules=[\n",
|
| 2603 |
+
" \"attn.to_q\", \"attn.to_k\", \"attn.to_v\",\n",
|
| 2604 |
+
" \"ff.linear_in\", \"ff.linear_out\",\n",
|
| 2605 |
+
" \"ff_context.linear_in\", \"ff_context.linear_out\",\n",
|
| 2606 |
+
" \"attn.to_qkv_mlp_proj\",\n",
|
| 2607 |
+
"],\n",
|
| 2608 |
+
" lora_dropout=0.05,\n",
|
| 2609 |
+
" bias=\"none\",\n",
|
| 2610 |
+
")\n",
|
| 2611 |
+
"#----#\n",
|
| 2612 |
+
"from peft import get_peft_model\n",
|
| 2613 |
+
"pipe.transformer = get_peft_model(pipe.transformer, config)\n",
|
| 2614 |
+
"pipe.transformer.print_trainable_parameters()"
|
| 2615 |
],
|
| 2616 |
"metadata": {
|
| 2617 |
"colab": {
|
|
|
|
| 2774 |
}
|
| 2775 |
]
|
| 2776 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2777 |
{
|
| 2778 |
"cell_type": "code",
|
| 2779 |
"source": [
|
|
|
|
| 2812 |
"\n",
|
| 2813 |
" def __getitem__(self, idx):\n",
|
| 2814 |
" image = self.transform(self.images[idx])\n",
|
| 2815 |
+
" return image, self.prompts[idx]"
|
| 2816 |
+
],
|
| 2817 |
+
"metadata": {
|
| 2818 |
+
"id": "7LIOh4CwyxIK"
|
| 2819 |
+
},
|
| 2820 |
+
"execution_count": null,
|
| 2821 |
+
"outputs": []
|
| 2822 |
+
},
|
| 2823 |
+
{
|
| 2824 |
+
"cell_type": "code",
|
| 2825 |
+
"source": [
|
| 2826 |
"\n",
|
| 2827 |
"dataset = Flux2KleinDataset(image_paths, instance_prompts, resolution=512)\n",
|
| 2828 |
"dataloader = DataLoader(dataset, batch_size=1, shuffle=True)\n",
|
|
|
|
| 2830 |
"# ====================== TRAINING SETUP ======================\n",
|
| 2831 |
"device = pipe.device\n",
|
| 2832 |
"dtype = torch.float16 # or bfloat16 if supported\n",
|
| 2833 |
+
"from sdnq.training import convert_sdnq_model_to_training\n",
|
| 2834 |
+
"from sdnq.common import use_torch_compile as triton_is_available\n",
|
| 2835 |
+
"pipe.transformer = convert_sdnq_model_to_training(\n",
|
| 2836 |
+
" pipe.transformer,\n",
|
| 2837 |
+
" quantized_matmul_dtype=\"int8\",\n",
|
| 2838 |
+
" use_grad_ckpt=True,\n",
|
| 2839 |
+
" use_quantized_matmul=False,\n",
|
| 2840 |
+
" use_stochastic_rounding=True,\n",
|
| 2841 |
+
" dequantize_fp32=True,\n",
|
| 2842 |
+
")\n",
|
| 2843 |
"\n",
|
| 2844 |
+
"#----#\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2845 |
"\n",
|
| 2846 |
+
"from sdnq.optim import Adafactor, AdamW, CAME, Lion, Muon\n",
|
| 2847 |
+
"optimizer = AdamW(\n",
|
| 2848 |
+
" parameters,\n",
|
| 2849 |
+
" use_quantized_buffers=True,\n",
|
| 2850 |
+
" quantized_buffers_dtype=\"uint8\",\n",
|
| 2851 |
+
" quantized_buffers_hadamard_group_size=128,\n",
|
| 2852 |
+
" quantized_buffers_group_size=32,\n",
|
| 2853 |
+
" quantized_buffers_svd_rank=32,\n",
|
| 2854 |
+
" final_norm_mode=\"clip\", # can be one of [\"none\", \"clip\", \"rms\", \"rms_clip\", \"relative\", \"muon\"]\n",
|
| 2855 |
+
" use_kahan=False,\n",
|
| 2856 |
+
" use_cautious=False,\n",
|
| 2857 |
+
" use_stochastic_rounding=True,\n",
|
| 2858 |
+
" use_stochastic_buffers=True,\n",
|
| 2859 |
+
" quantized_buffers_use_svd=False,\n",
|
| 2860 |
+
" quantized_buffers_use_hadamard=False,\n",
|
| 2861 |
+
" use_torch_compile=False,\n",
|
| 2862 |
+
" offload_buffers=False,\n",
|
| 2863 |
+
" offload_non_blocking=True,\n",
|
| 2864 |
+
")\n",
|
| 2865 |
+
"#----#\n",
|
| 2866 |
"num_epochs = 5\n",
|
| 2867 |
"accumulation_steps = 2\n",
|
| 2868 |
"\n",
|
|
|
|
| 2880 |
" with torch.no_grad():\n",
|
| 2881 |
" # FLUX VAE expects [-1, 1]\n",
|
| 2882 |
" latents = pipe.vae.encode(image * 2 - 1).latent_dist.sample()\n",
|
| 2883 |
+
" # No scaling factor should be used for flux klein 4B\n",
|
| 2884 |
"\n",
|
| 2885 |
" # ====================== TEXT ENCODING ======================\n",
|
| 2886 |
" with torch.no_grad():\n",
|