codeShare commited on
Commit
a418bbc
·
verified ·
1 Parent(s): c9417d6

Upload Create_SDNQ_from_klein_9b_transformer_model.ipynb

Browse files
colab_notebooks/Create_SDNQ_from_klein_9b_transformer_model.ipynb CHANGED
@@ -44,22 +44,10 @@
44
  "print(\"✅ Dependencies installed\")"
45
  ],
46
  "metadata": {
47
- "colab": {
48
- "base_uri": "https://localhost:8080/"
49
- },
50
- "id": "XXld_BQ1sU0v",
51
- "outputId": "38508641-2a6a-4a19-f585-d2ac2d2faef4"
52
  },
53
- "execution_count": 7,
54
- "outputs": [
55
- {
56
- "output_type": "stream",
57
- "name": "stdout",
58
- "text": [
59
- "✅ Dependencies installed\n"
60
- ]
61
- }
62
- ]
63
  },
64
  {
65
  "cell_type": "code",
@@ -128,7 +116,7 @@
128
  "metadata": {
129
  "id": "rQ5cr5ZsUQqG"
130
  },
131
- "execution_count": 8,
132
  "outputs": []
133
  },
134
  {
@@ -239,9 +227,9 @@
239
  "base_uri": "https://localhost:8080/"
240
  },
241
  "id": "aGH03dqUQcsQ",
242
- "outputId": "b5d8cd52-aa9b-49f6-c43b-0f9c75fe210f"
243
  },
244
- "execution_count": 10,
245
  "outputs": [
246
  {
247
  "output_type": "stream",
@@ -265,7 +253,7 @@
265
  "id": "EpL4Kek7AbjZ",
266
  "outputId": "9c46f98d-cbd0-498f-bb94-33b6860f49c6"
267
  },
268
- "execution_count": 7,
269
  "outputs": [
270
  {
271
  "output_type": "execute_result",
@@ -551,31 +539,39 @@
551
  "pipe.transformer = sdnq_post_load_quant(\n",
552
  " pipe.transformer,\n",
553
  " use_dynamic_quantization=True,\n",
554
- " weights_dtype=\"uint4\",\n",
555
- " dynamic_loss_threshold=1e-2,\n",
556
  " use_svd=True,\n",
557
  " group_size=0,\n",
 
558
  " quantization_device=\"cuda\",\n",
559
  " return_device=\"cpu\",\n",
560
  " quant_conv=False,\n",
561
  " quant_embedding=False,\n",
562
  ")\n",
563
  "\n",
564
- "import torch\n",
565
  "with torch.no_grad():\n",
566
  " transformer_save_path = \"/content/transformer\"\n",
567
- " pipe.transformer.save_pretrained(transformer_save_path,safe_serialization=True , max_shard_size='2GB')\n",
568
- " print(f\"✅ pipe.transformer saved to: {transformer_save_path}\")"
 
 
 
569
  ],
570
  "metadata": {
571
  "id": "hWAkx7u3XfdY"
572
  },
573
- "execution_count": 12,
574
  "outputs": []
575
  },
576
  {
577
  "cell_type": "code",
578
  "source": [
 
 
 
 
579
  "import torch, gc\n",
580
  "from sdnq import SDNQConfig , sdnq_post_load_quant\n",
581
  "\n",
@@ -584,8 +580,8 @@
584
  " pipe.text_encoder,\n",
585
  " use_dynamic_quantization=True,\n",
586
  " weights_dtype=\"uint2\",\n",
587
- " dynamic_loss_threshold=1e-2,\n",
588
- " use_svd=True,\n",
589
  " group_size=0,\n",
590
  " quantization_device=\"cuda\",\n",
591
  " return_device=\"cpu\",\n",
@@ -596,13 +592,16 @@
596
  "import torch\n",
597
  "with torch.no_grad():\n",
598
  " text_encoder_save_path = \"/content/text_encoder\"\n",
599
- " pipe.text_encoder.save_pretrained(text_encoder_save_path,safe_serialization=True , max_shard_size='2GB')\n",
600
- " print(f\"✅ pipe.text_encoder saved to: {text_encoder_save_path}\")"
 
 
 
601
  ],
602
  "metadata": {
603
  "id": "uSPLPAVwgXp_"
604
  },
605
- "execution_count": 10,
606
  "outputs": []
607
  },
608
  {
@@ -615,7 +614,7 @@
615
  "metadata": {
616
  "id": "Xvnz0s2AV0VW"
617
  },
618
- "execution_count": 3,
619
  "outputs": []
620
  },
621
  {
@@ -627,7 +626,7 @@
627
  "import torch\n",
628
  "with torch.no_grad():\n",
629
  " vae_save_path = \"/content/vae\"\n",
630
- " pipe.vae.save_pretrained(vae_save_path,safe_serialization=True , max_shard_size='2GB')\n",
631
  " print(f\"✅ pipe.vae saved to: {vae_save_path}\")"
632
  ],
633
  "metadata": {
@@ -635,9 +634,9 @@
635
  "base_uri": "https://localhost:8080/"
636
  },
637
  "id": "4PBgpLyDlwn4",
638
- "outputId": "b9419cc2-5b2d-4909-980d-c424b00e86e0"
639
  },
640
- "execution_count": 11,
641
  "outputs": [
642
  {
643
  "output_type": "stream",
@@ -654,7 +653,7 @@
654
  "import torch\n",
655
  "with torch.no_grad():\n",
656
  " scheduler_save_path = \"/content/scheduler\"\n",
657
- " pipe.scheduler.save_pretrained(scheduler_save_path,safe_serialization=True , max_shard_size='2GB')\n",
658
  " print(f\"✅ pipe.scheduler saved to: {scheduler_save_path}\")"
659
  ],
660
  "metadata": {
@@ -662,9 +661,9 @@
662
  "base_uri": "https://localhost:8080/"
663
  },
664
  "id": "nxJZeqwlmgcR",
665
- "outputId": "e4155232-bd12-48fc-b80d-db1358c6f1fb"
666
  },
667
- "execution_count": 12,
668
  "outputs": [
669
  {
670
  "output_type": "stream",
@@ -681,7 +680,7 @@
681
  "import torch\n",
682
  "with torch.no_grad():\n",
683
  " tokenizer_save_path = \"/content/tokenizer\"\n",
684
- " pipe.tokenizer.save_pretrained(tokenizer_save_path,safe_serialization=True , max_shard_size='2GB')\n",
685
  " print(f\"✅ pipe.tokenizer saved to: {tokenizer_save_path}\")"
686
  ],
687
  "metadata": {
@@ -689,9 +688,9 @@
689
  "base_uri": "https://localhost:8080/"
690
  },
691
  "id": "ZGOf4WtqFH6g",
692
- "outputId": "cac3ffff-efd6-4255-c7e5-86ae75921a00"
693
  },
694
- "execution_count": 13,
695
  "outputs": [
696
  {
697
  "output_type": "stream",
@@ -1021,8 +1020,7 @@
1021
  "api = HfApi()\n",
1022
  "\n",
1023
  "# Define the target repository ID\n",
1024
- "repo_id = \"codeShare/FLUX.2-klein-9b-SDNQ-2bit\" # Or \"codeShare/FLUX.2-klein-9b-SDNQ-4bit\" if that was the intended repo\n",
1025
- "\n",
1026
  "# Check if repository exists, if not, create it\n",
1027
  "print(f\"Checking if repository {repo_id} exists...\")\n",
1028
  "try:\n",
@@ -1079,6 +1077,126 @@
1079
  ],
1080
  "execution_count": null,
1081
  "outputs": []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1082
  }
1083
  ],
1084
  "metadata": {
 
44
  "print(\"✅ Dependencies installed\")"
45
  ],
46
  "metadata": {
47
+ "id": "XXld_BQ1sU0v"
 
 
 
 
48
  },
49
+ "execution_count": null,
50
+ "outputs": []
 
 
 
 
 
 
 
 
51
  },
52
  {
53
  "cell_type": "code",
 
116
  "metadata": {
117
  "id": "rQ5cr5ZsUQqG"
118
  },
119
+ "execution_count": 4,
120
  "outputs": []
121
  },
122
  {
 
227
  "base_uri": "https://localhost:8080/"
228
  },
229
  "id": "aGH03dqUQcsQ",
230
+ "outputId": "52fdb852-4c99-4e2c-f13e-7bfe0e62f511"
231
  },
232
+ "execution_count": 6,
233
  "outputs": [
234
  {
235
  "output_type": "stream",
 
253
  "id": "EpL4Kek7AbjZ",
254
  "outputId": "9c46f98d-cbd0-498f-bb94-33b6860f49c6"
255
  },
256
+ "execution_count": null,
257
  "outputs": [
258
  {
259
  "output_type": "execute_result",
 
539
  "pipe.transformer = sdnq_post_load_quant(\n",
540
  " pipe.transformer,\n",
541
  " use_dynamic_quantization=True,\n",
542
+ " weights_dtype=\"uint2\",\n",
543
+ " dynamic_loss_threshold=1e-1,\n",
544
  " use_svd=True,\n",
545
  " group_size=0,\n",
546
+ " svd_steps=16,\n",
547
  " quantization_device=\"cuda\",\n",
548
  " return_device=\"cpu\",\n",
549
  " quant_conv=False,\n",
550
  " quant_embedding=False,\n",
551
  ")\n",
552
  "\n",
553
+ "import torch , gc\n",
554
  "with torch.no_grad():\n",
555
  " transformer_save_path = \"/content/transformer\"\n",
556
+ " pipe.transformer.save_pretrained(transformer_save_path,safe_serialization=True)\n",
557
+ " print(f\"✅ pipe.transformer saved to: {transformer_save_path}\")\n",
558
+ "#----#\n",
559
+ "gc.collect()\n",
560
+ "torch.cuda.empty_cache()"
561
  ],
562
  "metadata": {
563
  "id": "hWAkx7u3XfdY"
564
  },
565
+ "execution_count": null,
566
  "outputs": []
567
  },
568
  {
569
  "cell_type": "code",
570
  "source": [
571
+ "import gc , torch\n",
572
+ "gc.collect()\n",
573
+ "torch.cuda.empty_cache()\n",
574
+ "\n",
575
  "import torch, gc\n",
576
  "from sdnq import SDNQConfig , sdnq_post_load_quant\n",
577
  "\n",
 
580
  " pipe.text_encoder,\n",
581
  " use_dynamic_quantization=True,\n",
582
  " weights_dtype=\"uint2\",\n",
583
+ " dynamic_loss_threshold=1e-1,\n",
584
+ " use_svd=False,\n",
585
  " group_size=0,\n",
586
  " quantization_device=\"cuda\",\n",
587
  " return_device=\"cpu\",\n",
 
592
  "import torch\n",
593
  "with torch.no_grad():\n",
594
  " text_encoder_save_path = \"/content/text_encoder\"\n",
595
+ " pipe.text_encoder.save_pretrained(text_encoder_save_path,safe_serialization=True)\n",
596
+ " print(f\"✅ pipe.text_encoder saved to: {text_encoder_save_path}\")\n",
597
+ "#----#\n",
598
+ "gc.collect()\n",
599
+ "torch.cuda.empty_cache()"
600
  ],
601
  "metadata": {
602
  "id": "uSPLPAVwgXp_"
603
  },
604
+ "execution_count": null,
605
  "outputs": []
606
  },
607
  {
 
614
  "metadata": {
615
  "id": "Xvnz0s2AV0VW"
616
  },
617
+ "execution_count": null,
618
  "outputs": []
619
  },
620
  {
 
626
  "import torch\n",
627
  "with torch.no_grad():\n",
628
  " vae_save_path = \"/content/vae\"\n",
629
+ " pipe.vae.save_pretrained(vae_save_path,safe_serialization=True)\n",
630
  " print(f\"✅ pipe.vae saved to: {vae_save_path}\")"
631
  ],
632
  "metadata": {
 
634
  "base_uri": "https://localhost:8080/"
635
  },
636
  "id": "4PBgpLyDlwn4",
637
+ "outputId": "eab87add-9978-45db-e585-ced9498ee6c8"
638
  },
639
+ "execution_count": 9,
640
  "outputs": [
641
  {
642
  "output_type": "stream",
 
653
  "import torch\n",
654
  "with torch.no_grad():\n",
655
  " scheduler_save_path = \"/content/scheduler\"\n",
656
+ " pipe.scheduler.save_pretrained(scheduler_save_path,safe_serialization=True)\n",
657
  " print(f\"✅ pipe.scheduler saved to: {scheduler_save_path}\")"
658
  ],
659
  "metadata": {
 
661
  "base_uri": "https://localhost:8080/"
662
  },
663
  "id": "nxJZeqwlmgcR",
664
+ "outputId": "1bfff9be-6236-4663-ff8a-ffb4394be809"
665
  },
666
+ "execution_count": 10,
667
  "outputs": [
668
  {
669
  "output_type": "stream",
 
680
  "import torch\n",
681
  "with torch.no_grad():\n",
682
  " tokenizer_save_path = \"/content/tokenizer\"\n",
683
+ " pipe.tokenizer.save_pretrained(tokenizer_save_path,safe_serialization=True)\n",
684
  " print(f\"✅ pipe.tokenizer saved to: {tokenizer_save_path}\")"
685
  ],
686
  "metadata": {
 
688
  "base_uri": "https://localhost:8080/"
689
  },
690
  "id": "ZGOf4WtqFH6g",
691
+ "outputId": "571daca1-dcf9-44a8-ecd7-9c460276964f"
692
  },
693
+ "execution_count": 11,
694
  "outputs": [
695
  {
696
  "output_type": "stream",
 
1020
  "api = HfApi()\n",
1021
  "\n",
1022
  "# Define the target repository ID\n",
1023
+ "repo_id = \"codeShare/FLUX.2-klein-9b-SDNQ-2bit\"\n",
 
1024
  "# Check if repository exists, if not, create it\n",
1025
  "print(f\"Checking if repository {repo_id} exists...\")\n",
1026
  "try:\n",
 
1077
  ],
1078
  "execution_count": null,
1079
  "outputs": []
1080
+ },
1081
+ {
1082
+ "cell_type": "code",
1083
+ "metadata": {
1084
+ "id": "f58c3a41"
1085
+ },
1086
+ "source": [
1087
+ "import os\n",
1088
+ "from huggingface_hub import HfApi, login\n",
1089
+ "from google.colab import userdata\n",
1090
+ "\n",
1091
+ "hf_token = userdata.get(\"HF_TOKEN\")\n",
1092
+ "login(token=hf_token)\n",
1093
+ "\n",
1094
+ "api = HfApi()\n",
1095
+ "\n",
1096
+ "repo_id = \"codeShare/FLUX.2-klein-9b-SDNQ-2bit\"\n",
1097
+ "source_folder_path = \"/content/transformer\"\n",
1098
+ "target_folder_in_repo = \"transformer\"\n",
1099
+ "\n",
1100
+ "print(f\"\\nUploading folder: {source_folder_path} to repo path: {target_folder_in_repo} in {repo_id}...\")\n",
1101
+ "\n",
1102
+ "if os.path.isdir(source_folder_path):\n",
1103
+ " api.upload_folder(\n",
1104
+ " folder_path=source_folder_path,\n",
1105
+ " repo_id=repo_id,\n",
1106
+ " repo_type=\"model\",\n",
1107
+ " path_in_repo=target_folder_in_repo,\n",
1108
+ " commit_message=f\"Upload {target_folder_in_repo} component\"\n",
1109
+ " )\n",
1110
+ " print(f\"✅ Folder {source_folder_path} uploaded to {repo_id}/{target_folder_in_repo}.\")\n",
1111
+ "else:\n",
1112
+ " print(f\"⚠️ Source folder not found, skipping: {source_folder_path}\")\n",
1113
+ "\n",
1114
+ "print(\"\\n✅ Transformer component upload process complete.\")"
1115
+ ],
1116
+ "execution_count": null,
1117
+ "outputs": []
1118
+ },
1119
+ {
1120
+ "cell_type": "code",
1121
+ "metadata": {
1122
+ "id": "ae55ccc9"
1123
+ },
1124
+ "source": [
1125
+ "import os\n",
1126
+ "from huggingface_hub import HfApi, login\n",
1127
+ "from google.colab import userdata\n",
1128
+ "\n",
1129
+ "hf_token = userdata.get(\"HF_TOKEN\")\n",
1130
+ "login(token=hf_token)\n",
1131
+ "\n",
1132
+ "api = HfApi()\n",
1133
+ "\n",
1134
+ "repo_id = \"codeShare/FLUX.2-klein-9b-SDNQ-2bit\"\n",
1135
+ "source_folder_path = \"/content/text_encoder\"\n",
1136
+ "target_folder_in_repo = \"text_encoder\"\n",
1137
+ "\n",
1138
+ "print(f\"\\nUploading folder: {source_folder_path} to repo path: {target_folder_in_repo} in {repo_id}...\")\n",
1139
+ "\n",
1140
+ "if os.path.isdir(source_folder_path):\n",
1141
+ " api.upload_folder(\n",
1142
+ " folder_path=source_folder_path,\n",
1143
+ " repo_id=repo_id,\n",
1144
+ " repo_type=\"model\",\n",
1145
+ " path_in_repo=target_folder_in_repo,\n",
1146
+ " commit_message=f\"Upload {target_folder_in_repo} component\"\n",
1147
+ " )\n",
1148
+ " print(f\"✅ Folder {source_folder_path} uploaded to {repo_id}/{target_folder_in_repo}.\")\n",
1149
+ "else:\n",
1150
+ " print(f\"⚠️ Source folder not found, skipping: {source_folder_path}\")\n",
1151
+ "\n",
1152
+ "print(\"\\n✅ Text Encoder component upload process complete.\")"
1153
+ ],
1154
+ "execution_count": null,
1155
+ "outputs": []
1156
+ },
1157
+ {
1158
+ "cell_type": "code",
1159
+ "metadata": {
1160
+ "id": "10131a92"
1161
+ },
1162
+ "source": [
1163
+ "import os\n",
1164
+ "from huggingface_hub import HfApi, login\n",
1165
+ "from google.colab import userdata\n",
1166
+ "\n",
1167
+ "hf_token = userdata.get(\"HF_TOKEN\")\n",
1168
+ "login(token=hf_token)\n",
1169
+ "\n",
1170
+ "api = HfApi()\n",
1171
+ "\n",
1172
+ "repo_id = \"codeShare/FLUX.2-klein-9b-SDNQ-2bit\"\n",
1173
+ "\n",
1174
+ "folders_to_upload = {\n",
1175
+ " \"/content/vae\": \"vae\",\n",
1176
+ " \"/content/scheduler\": \"scheduler\",\n",
1177
+ " \"/content/tokenizer\": \"tokenizer\",\n",
1178
+ "}\n",
1179
+ "\n",
1180
+ "print(f\"\\nStarting upload of vae, scheduler, and tokenizer components to {repo_id}...\")\n",
1181
+ "\n",
1182
+ "for source_folder, target_folder_in_repo in folders_to_upload.items():\n",
1183
+ " print(f\"Uploading folder: {source_folder} to repo path: {target_folder_in_repo} in {repo_id}...\")\n",
1184
+ " if os.path.isdir(source_folder):\n",
1185
+ " api.upload_folder(\n",
1186
+ " folder_path=source_folder,\n",
1187
+ " repo_id=repo_id,\n",
1188
+ " repo_type=\"model\",\n",
1189
+ " path_in_repo=target_folder_in_repo,\n",
1190
+ " commit_message=f\"Upload {target_folder_in_repo} component\"\n",
1191
+ " )\n",
1192
+ " print(f\"✅ Folder {source_folder} uploaded to {repo_id}/{target_folder_in_repo}.\")\n",
1193
+ " else:\n",
1194
+ " print(f\"⚠️ Source folder not found, skipping: {source_folder}\")\n",
1195
+ "\n",
1196
+ "print(\"\\n✅ All requested components upload process complete.\")"
1197
+ ],
1198
+ "execution_count": null,
1199
+ "outputs": []
1200
  }
1201
  ],
1202
  "metadata": {