codeShare commited on
Commit
ba4cb6c
·
verified ·
1 Parent(s): 136ec2a

Upload Create_SDNQ_from_klein_9b_transformer_model.ipynb

Browse files
colab_notebooks/Create_SDNQ_from_klein_9b_transformer_model.ipynb ADDED
@@ -0,0 +1,1100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "source": [
6
+ "# create SDNQ"
7
+ ],
8
+ "metadata": {
9
+ "id": "9vXxafECsKC_"
10
+ }
11
+ },
12
+ {
13
+ "cell_type": "code",
14
+ "source": [
15
+ "#@markdown # CELL 1 — Setup environment + login\n",
16
+ "\n",
17
+ "from google.colab import drive, userdata\n",
18
+ "from huggingface_hub import login\n",
19
+ "import torch, os, gc\n",
20
+ "\n",
21
+ "drive.mount(\"/content/drive\")\n",
22
+ "\n",
23
+ "hf_token = userdata.get(\"HF_TOKEN\")\n",
24
+ "if hf_token:\n",
25
+ " login(token=hf_token)\n",
26
+ "else:\n",
27
+ " raise ValueError(\"HF_TOKEN not found in Google Colab secrets\")\n",
28
+ "\n",
29
+ "print(\"✅ Logged into Hugging Face\")"
30
+ ],
31
+ "metadata": {
32
+ "id": "XzgpnXJhsHg-"
33
+ },
34
+ "execution_count": null,
35
+ "outputs": []
36
+ },
37
+ {
38
+ "cell_type": "code",
39
+ "source": [
40
+ "#@markdown # CELL 2 — Install dependencies\n",
41
+ "\n",
42
+ "!pip install -q safetensors huggingface_hub diffusers transformers accelerate\n",
43
+ "\n",
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",
66
+ "source": [
67
+ "import torch\n",
68
+ "from transformers import BitsAndBytesConfig\n",
69
+ "from diffusers import DiffusionPipeline\n",
70
+ "\n",
71
+ "# ============================================================\n",
72
+ "# BNB CONFIG\n",
73
+ "# ============================================================\n",
74
+ "\n",
75
+ "bnb_config = BitsAndBytesConfig(\n",
76
+ " load_in_4bit=True,\n",
77
+ " bnb_4bit_quant_type=\"nf4\",\n",
78
+ " bnb_4bit_compute_dtype=torch.bfloat16,\n",
79
+ " bnb_4bit_use_double_quant=True,\n",
80
+ ")\n",
81
+ "\n",
82
+ "# ============================================================\n",
83
+ "# LOAD PIPELINE\n",
84
+ "# ============================================================\n",
85
+ "\n",
86
+ "pipe = DiffusionPipeline.from_pretrained(\n",
87
+ " \"black-forest-labs/FLUX.2-klein-9B\",\n",
88
+ " # Removed: quantization_config=bnb_config,\n",
89
+ " torch_dtype=torch.bfloat16,\n",
90
+ "\n",
91
+ " # Pass individual quantization parameters directly\n",
92
+ " load_in_4bit=bnb_config.load_in_4bit,\n",
93
+ " bnb_4bit_quant_type=bnb_config.bnb_4bit_quant_type,\n",
94
+ " bnb_4bit_compute_dtype=bnb_config.bnb_4bit_compute_dtype,\n",
95
+ " bnb_4bit_use_double_quant=bnb_config.bnb_4bit_use_double_quant,\n",
96
+ "\n",
97
+ " # critical:\n",
98
+ " device_map=\"cpu\", # Changed 'auto' to 'cpu'\n",
99
+ "\n",
100
+ " # enables CPU offload folder\n",
101
+ " offload_folder=\"/content/offload\",\n",
102
+ "\n",
103
+ " # low RAM loading\n",
104
+ " low_cpu_mem_usage=True,\n",
105
+ ")\n",
106
+ "\n",
107
+ "# ============================================================\n",
108
+ "# ENABLE FULL CPU OFFLOAD\n",
109
+ "# ============================================================\n",
110
+ "\n",
111
+ "pipe.enable_model_cpu_offload()\n",
112
+ "\n",
113
+ "print(\"✅ Loaded with bitsandbytes + CPU offload\")"
114
+ ],
115
+ "metadata": {
116
+ "id": "i9MbakNlsckw"
117
+ },
118
+ "execution_count": null,
119
+ "outputs": []
120
+ },
121
+ {
122
+ "cell_type": "code",
123
+ "source": [
124
+ "import gc , torch\n",
125
+ "gc.collect()\n",
126
+ "torch.cuda.empty_cache()"
127
+ ],
128
+ "metadata": {
129
+ "id": "rQ5cr5ZsUQqG"
130
+ },
131
+ "execution_count": 8,
132
+ "outputs": []
133
+ },
134
+ {
135
+ "cell_type": "code",
136
+ "source": [
137
+ "!pip install bitsandbytes\n",
138
+ "import torch\n",
139
+ "import gc\n",
140
+ "import bitsandbytes as bnb\n",
141
+ "\n",
142
+ "from torch import nn\n",
143
+ "\n",
144
+ "# ============================================================\n",
145
+ "# DEQUANTIZE BNB MODEL\n",
146
+ "# ============================================================\n",
147
+ "\n",
148
+ "def dequantize_bnb_linear(layer):\n",
149
+ " \"\"\"\n",
150
+ " Convert Linear4bit -> torch.nn.Linear\n",
151
+ " \"\"\"\n",
152
+ "\n",
153
+ " # Reconstruct full precision weights\n",
154
+ " weight = layer.weight.dequantize()\n",
155
+ "\n",
156
+ " new_layer = nn.Linear(\n",
157
+ " layer.in_features,\n",
158
+ " layer.out_features,\n",
159
+ " bias=layer.bias is not None,\n",
160
+ " dtype=weight.dtype,\n",
161
+ " device=\"cpu\",\n",
162
+ " )\n",
163
+ "\n",
164
+ " new_layer.weight.data.copy_(weight.cpu())\n",
165
+ "\n",
166
+ " if layer.bias is not None:\n",
167
+ " new_layer.bias.data.copy_(layer.bias.data.cpu())\n",
168
+ "\n",
169
+ " return new_layer\n",
170
+ "\n",
171
+ "\n",
172
+ "def recursively_dequantize(module):\n",
173
+ " \"\"\"\n",
174
+ " Recursively replace all BNB 4bit layers.\n",
175
+ " \"\"\"\n",
176
+ "\n",
177
+ " for name, child in list(module.named_children()):\n",
178
+ "\n",
179
+ " # Replace Linear4bit\n",
180
+ " if isinstance(child, bnb.nn.Linear4bit):\n",
181
+ " print(f\"Dequantizing: {name}\")\n",
182
+ "\n",
183
+ " setattr(\n",
184
+ " module,\n",
185
+ " name,\n",
186
+ " dequantize_bnb_linear(child)\n",
187
+ " )\n",
188
+ "\n",
189
+ " else:\n",
190
+ " recursively_dequantize(child)\n",
191
+ "\n",
192
+ "\n",
193
+ "# ============================================================\n",
194
+ "# RUN DEQUANTIZATION\n",
195
+ "# ============================================================\n",
196
+ "\n",
197
+ "pipe.to(\"cpu\")\n",
198
+ "\n",
199
+ "gc.collect()\n",
200
+ "torch.cuda.empty_cache()\n",
201
+ "\n",
202
+ "# Apply dequantization to the individual sub-modules within the pipeline\n",
203
+ "print(\"Dequantizing pipe.transformer...\")\n",
204
+ "recursively_dequantize(pipe.transformer)\n",
205
+ "print(\"Dequantizing pipe.text_encoder...\")\n",
206
+ "recursively_dequantize(pipe.text_encoder)\n",
207
+ "print(\"Dequantizing pipe.vae...\")\n",
208
+ "recursively_dequantize(pipe.vae)\n",
209
+ "\n",
210
+ "gc.collect()\n",
211
+ "torch.cuda.empty_cache()\n",
212
+ "\n",
213
+ "print(\"✅ Fully dequantized back to FP weights\")"
214
+ ],
215
+ "metadata": {
216
+ "id": "GbZh_p6zSeTy"
217
+ },
218
+ "execution_count": null,
219
+ "outputs": []
220
+ },
221
+ {
222
+ "cell_type": "code",
223
+ "source": [
224
+ "#print(\"🧹 Removing old diffusers...\")\n",
225
+ "#!pip uninstall -y diffusers > /dev/null 2>&1\n",
226
+ "#!rm -rf /usr/local/lib/python3.12/dist-packages/diffusers* ~/.cache/pip/*diffusers*\n",
227
+ "\n",
228
+ "!pip install -q --upgrade huggingface_hub transformers accelerate diffusers\n",
229
+ "!pip install -q sdnq\n",
230
+ "\n",
231
+ "#print(\"🔄 Installing latest diffusers...\")\n",
232
+ "#!pip install -q git+https://github.com/huggingface/diffusers.git --force-reinstall --no-deps\n",
233
+ "#!python -m pip cache purge\n",
234
+ "\n",
235
+ "print(\"✅ Cell 1 complete!\")"
236
+ ],
237
+ "metadata": {
238
+ "colab": {
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",
248
+ "name": "stdout",
249
+ "text": [
250
+ "✅ Cell 1 complete!\n"
251
+ ]
252
+ }
253
+ ]
254
+ },
255
+ {
256
+ "cell_type": "code",
257
+ "source": [
258
+ "from sdnq.common import accepted_weight_dtypes\n",
259
+ "accepted_weight_dtypes"
260
+ ],
261
+ "metadata": {
262
+ "colab": {
263
+ "base_uri": "https://localhost:8080/"
264
+ },
265
+ "id": "EpL4Kek7AbjZ",
266
+ "outputId": "9c46f98d-cbd0-498f-bb94-33b6860f49c6"
267
+ },
268
+ "execution_count": 7,
269
+ "outputs": [
270
+ {
271
+ "output_type": "execute_result",
272
+ "data": {
273
+ "text/plain": [
274
+ "{'bf16',\n",
275
+ " 'bfloat16',\n",
276
+ " 'bool',\n",
277
+ " 'float10_e1m8fn',\n",
278
+ " 'float10_e1m9fnu',\n",
279
+ " 'float10_e2m7fn',\n",
280
+ " 'float10_e2m8fnu',\n",
281
+ " 'float10_e3m6fn',\n",
282
+ " 'float10_e3m7fnu',\n",
283
+ " 'float10_e4m5fn',\n",
284
+ " 'float10_e4m6fnu',\n",
285
+ " 'float10_e5m4fn',\n",
286
+ " 'float10_e5m5fnu',\n",
287
+ " 'float11_e1m10fnu',\n",
288
+ " 'float11_e1m9fn',\n",
289
+ " 'float11_e2m8fn',\n",
290
+ " 'float11_e2m9fnu',\n",
291
+ " 'float11_e3m7fn',\n",
292
+ " 'float11_e3m8fnu',\n",
293
+ " 'float11_e4m6fn',\n",
294
+ " 'float11_e4m7fnu',\n",
295
+ " 'float11_e5m5fn',\n",
296
+ " 'float11_e5m6fnu',\n",
297
+ " 'float12_e1m10fn',\n",
298
+ " 'float12_e1m11fnu',\n",
299
+ " 'float12_e2m10fnu',\n",
300
+ " 'float12_e2m9fn',\n",
301
+ " 'float12_e3m8fn',\n",
302
+ " 'float12_e3m9fnu',\n",
303
+ " 'float12_e4m7fn',\n",
304
+ " 'float12_e4m8fnu',\n",
305
+ " 'float12_e5m6fn',\n",
306
+ " 'float12_e5m7fnu',\n",
307
+ " 'float13_e1m11fn',\n",
308
+ " 'float13_e1m12fnu',\n",
309
+ " 'float13_e2m10fn',\n",
310
+ " 'float13_e2m11fnu',\n",
311
+ " 'float13_e3m10fnu',\n",
312
+ " 'float13_e3m9fn',\n",
313
+ " 'float13_e4m8fn',\n",
314
+ " 'float13_e4m9fnu',\n",
315
+ " 'float13_e5m7fn',\n",
316
+ " 'float13_e5m8fnu',\n",
317
+ " 'float14_e1m12fn',\n",
318
+ " 'float14_e1m13fnu',\n",
319
+ " 'float14_e2m11fn',\n",
320
+ " 'float14_e2m12fnu',\n",
321
+ " 'float14_e3m10fn',\n",
322
+ " 'float14_e3m11fnu',\n",
323
+ " 'float14_e4m10fnu',\n",
324
+ " 'float14_e4m9fn',\n",
325
+ " 'float14_e5m8fn',\n",
326
+ " 'float14_e5m9fnu',\n",
327
+ " 'float15_e1m13fn',\n",
328
+ " 'float15_e1m14fnu',\n",
329
+ " 'float15_e2m12fn',\n",
330
+ " 'float15_e2m13fnu',\n",
331
+ " 'float15_e3m11fn',\n",
332
+ " 'float15_e3m12fnu',\n",
333
+ " 'float15_e4m10fn',\n",
334
+ " 'float15_e4m11fnu',\n",
335
+ " 'float15_e5m10fnu',\n",
336
+ " 'float15_e5m9fn',\n",
337
+ " 'float16',\n",
338
+ " 'float16_e1m14fn',\n",
339
+ " 'float16_e1m15fnu',\n",
340
+ " 'float16_e2m13fn',\n",
341
+ " 'float16_e2m14fnu',\n",
342
+ " 'float16_e3m12fn',\n",
343
+ " 'float16_e3m13fnu',\n",
344
+ " 'float16_e4m11fn',\n",
345
+ " 'float16_e4m12fnu',\n",
346
+ " 'float16_e5m10fn',\n",
347
+ " 'float16_e5m11fnu',\n",
348
+ " 'float1_e1m0fnu',\n",
349
+ " 'float2_e1m0fn',\n",
350
+ " 'float2_e1m1fnu',\n",
351
+ " 'float2_e2m0fnu',\n",
352
+ " 'float32',\n",
353
+ " 'float3_e1m1fn',\n",
354
+ " 'float3_e1m2fnu',\n",
355
+ " 'float3_e2m0fn',\n",
356
+ " 'float3_e2m1fnu',\n",
357
+ " 'float3_e3m0fnu',\n",
358
+ " 'float4_e1m2fn',\n",
359
+ " 'float4_e1m3fnu',\n",
360
+ " 'float4_e2m1fn',\n",
361
+ " 'float4_e2m2fnu',\n",
362
+ " 'float4_e3m0fn',\n",
363
+ " 'float4_e3m1fnu',\n",
364
+ " 'float4_e4m0fnu',\n",
365
+ " 'float5_e1m3fn',\n",
366
+ " 'float5_e1m4fnu',\n",
367
+ " 'float5_e2m2fn',\n",
368
+ " 'float5_e2m3fnu',\n",
369
+ " 'float5_e3m1fn',\n",
370
+ " 'float5_e3m2fnu',\n",
371
+ " 'float5_e4m0fn',\n",
372
+ " 'float5_e4m1fnu',\n",
373
+ " 'float5_e5m0fnu',\n",
374
+ " 'float6_e1m4fn',\n",
375
+ " 'float6_e1m5fnu',\n",
376
+ " 'float6_e2m3fn',\n",
377
+ " 'float6_e2m4fnu',\n",
378
+ " 'float6_e3m2fn',\n",
379
+ " 'float6_e3m3fnu',\n",
380
+ " 'float6_e4m1fn',\n",
381
+ " 'float6_e4m2fnu',\n",
382
+ " 'float6_e5m0fn',\n",
383
+ " 'float6_e5m1fnu',\n",
384
+ " 'float7_e1m5fn',\n",
385
+ " 'float7_e1m6fnu',\n",
386
+ " 'float7_e2m4fn',\n",
387
+ " 'float7_e2m5fnu',\n",
388
+ " 'float7_e3m3fn',\n",
389
+ " 'float7_e3m4fnu',\n",
390
+ " 'float7_e4m2fn',\n",
391
+ " 'float7_e4m3fnu',\n",
392
+ " 'float7_e5m1fn',\n",
393
+ " 'float7_e5m2fnu',\n",
394
+ " 'float8_e1m6fn',\n",
395
+ " 'float8_e1m7fnu',\n",
396
+ " 'float8_e2m5fn',\n",
397
+ " 'float8_e2m6fnu',\n",
398
+ " 'float8_e3m4fn',\n",
399
+ " 'float8_e3m5fnu',\n",
400
+ " 'float8_e4m3fn',\n",
401
+ " 'float8_e4m3fn_sdnq',\n",
402
+ " 'float8_e4m3fnuz',\n",
403
+ " 'float8_e4m4fnu',\n",
404
+ " 'float8_e5m2',\n",
405
+ " 'float8_e5m2fn',\n",
406
+ " 'float8_e5m2fnuz',\n",
407
+ " 'float8_e5m3fnu',\n",
408
+ " 'float8_e8m0fnu',\n",
409
+ " 'float9_e1m7fn',\n",
410
+ " 'float9_e1m8fnu',\n",
411
+ " 'float9_e2m6fn',\n",
412
+ " 'float9_e2m7fnu',\n",
413
+ " 'float9_e3m5fn',\n",
414
+ " 'float9_e3m6fnu',\n",
415
+ " 'float9_e4m4fn',\n",
416
+ " 'float9_e4m5fnu',\n",
417
+ " 'float9_e5m3fn',\n",
418
+ " 'float9_e5m4fnu',\n",
419
+ " 'fp1',\n",
420
+ " 'fp10',\n",
421
+ " 'fp11',\n",
422
+ " 'fp12',\n",
423
+ " 'fp13',\n",
424
+ " 'fp14',\n",
425
+ " 'fp15',\n",
426
+ " 'fp16',\n",
427
+ " 'fp2',\n",
428
+ " 'fp3',\n",
429
+ " 'fp32',\n",
430
+ " 'fp4',\n",
431
+ " 'fp5',\n",
432
+ " 'fp6',\n",
433
+ " 'fp7',\n",
434
+ " 'fp8',\n",
435
+ " 'fp9',\n",
436
+ " 'int1',\n",
437
+ " 'int10',\n",
438
+ " 'int11',\n",
439
+ " 'int12',\n",
440
+ " 'int13',\n",
441
+ " 'int14',\n",
442
+ " 'int15',\n",
443
+ " 'int16',\n",
444
+ " 'int2',\n",
445
+ " 'int3',\n",
446
+ " 'int32',\n",
447
+ " 'int4',\n",
448
+ " 'int5',\n",
449
+ " 'int6',\n",
450
+ " 'int7',\n",
451
+ " 'int8',\n",
452
+ " 'int9',\n",
453
+ " 'ufp1',\n",
454
+ " 'ufp10',\n",
455
+ " 'ufp11',\n",
456
+ " 'ufp12',\n",
457
+ " 'ufp13',\n",
458
+ " 'ufp14',\n",
459
+ " 'ufp15',\n",
460
+ " 'ufp16',\n",
461
+ " 'ufp2',\n",
462
+ " 'ufp3',\n",
463
+ " 'ufp4',\n",
464
+ " 'ufp5',\n",
465
+ " 'ufp6',\n",
466
+ " 'ufp7',\n",
467
+ " 'ufp8',\n",
468
+ " 'ufp9',\n",
469
+ " 'uint1',\n",
470
+ " 'uint10',\n",
471
+ " 'uint11',\n",
472
+ " 'uint12',\n",
473
+ " 'uint13',\n",
474
+ " 'uint14',\n",
475
+ " 'uint15',\n",
476
+ " 'uint16',\n",
477
+ " 'uint2',\n",
478
+ " 'uint3',\n",
479
+ " 'uint32',\n",
480
+ " 'uint4',\n",
481
+ " 'uint5',\n",
482
+ " 'uint6',\n",
483
+ " 'uint7',\n",
484
+ " 'uint8',\n",
485
+ " 'uint9'}"
486
+ ]
487
+ },
488
+ "metadata": {},
489
+ "execution_count": 7
490
+ }
491
+ ]
492
+ },
493
+ {
494
+ "cell_type": "code",
495
+ "source": [
496
+ "pipe"
497
+ ],
498
+ "metadata": {
499
+ "colab": {
500
+ "base_uri": "https://localhost:8080/"
501
+ },
502
+ "id": "iJaa_thrWa22",
503
+ "outputId": "f8109023-9b9e-4b84-ba45-c518a1b36579"
504
+ },
505
+ "execution_count": null,
506
+ "outputs": [
507
+ {
508
+ "output_type": "execute_result",
509
+ "data": {
510
+ "text/plain": [
511
+ "Flux2KleinPipeline {\n",
512
+ " \"_class_name\": \"Flux2KleinPipeline\",\n",
513
+ " \"_diffusers_version\": \"0.38.0\",\n",
514
+ " \"_name_or_path\": \"black-forest-labs/FLUX.2-klein-9B\",\n",
515
+ " \"is_distilled\": true,\n",
516
+ " \"scheduler\": [\n",
517
+ " \"diffusers\",\n",
518
+ " \"FlowMatchEulerDiscreteScheduler\"\n",
519
+ " ],\n",
520
+ " \"text_encoder\": [\n",
521
+ " \"transformers\",\n",
522
+ " \"Qwen3ForCausalLM\"\n",
523
+ " ],\n",
524
+ " \"tokenizer\": [\n",
525
+ " \"transformers\",\n",
526
+ " \"Qwen2Tokenizer\"\n",
527
+ " ],\n",
528
+ " \"transformer\": [\n",
529
+ " \"diffusers\",\n",
530
+ " \"Flux2Transformer2DModel\"\n",
531
+ " ],\n",
532
+ " \"vae\": [\n",
533
+ " \"diffusers\",\n",
534
+ " \"AutoencoderKLFlux2\"\n",
535
+ " ]\n",
536
+ "}"
537
+ ]
538
+ },
539
+ "metadata": {},
540
+ "execution_count": 18
541
+ }
542
+ ]
543
+ },
544
+ {
545
+ "cell_type": "code",
546
+ "source": [
547
+ "import torch, gc\n",
548
+ "from sdnq import SDNQConfig , sdnq_post_load_quant\n",
549
+ "\n",
550
+ "# Apply SDNQ to transformer\n",
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",
582
+ "# Apply SDNQ to transformer\n",
583
+ "pipe.text_encoder = sdnq_post_load_quant(\n",
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",
592
+ " quant_conv=False,\n",
593
+ " quant_embedding=False,\n",
594
+ ")\n",
595
+ "\n",
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
+ {
609
+ "cell_type": "code",
610
+ "source": [
611
+ "import gc , torch\n",
612
+ "gc.collect()\n",
613
+ "torch.cuda.empty_cache()"
614
+ ],
615
+ "metadata": {
616
+ "id": "Xvnz0s2AV0VW"
617
+ },
618
+ "execution_count": 3,
619
+ "outputs": []
620
+ },
621
+ {
622
+ "cell_type": "code",
623
+ "source": [
624
+ "import torch, gc\n",
625
+ "from sdnq import SDNQConfig , sdnq_post_load_quant\n",
626
+ "\n",
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": {
634
+ "colab": {
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",
644
+ "name": "stdout",
645
+ "text": [
646
+ "✅ pipe.vae saved to: /content/vae\n"
647
+ ]
648
+ }
649
+ ]
650
+ },
651
+ {
652
+ "cell_type": "code",
653
+ "source": [
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": {
661
+ "colab": {
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",
671
+ "name": "stdout",
672
+ "text": [
673
+ "✅ pipe.scheduler saved to: /content/scheduler\n"
674
+ ]
675
+ }
676
+ ]
677
+ },
678
+ {
679
+ "cell_type": "code",
680
+ "source": [
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": {
688
+ "colab": {
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",
698
+ "name": "stdout",
699
+ "text": [
700
+ "✅ pipe.tokenizer saved to: /content/tokenizer\n"
701
+ ]
702
+ }
703
+ ]
704
+ },
705
+ {
706
+ "cell_type": "code",
707
+ "metadata": {
708
+ "id": "fbf8ead6"
709
+ },
710
+ "source": [
711
+ "import os\n",
712
+ "from huggingface_hub import HfApi, login, create_repo\n",
713
+ "from google.colab import userdata\n",
714
+ "from huggingface_hub.utils import HfHubHTTPError\n",
715
+ "\n",
716
+ "# Retrieve hf_token from Colab secrets\n",
717
+ "hf_token = userdata.get(\"HF_TOKEN\")\n",
718
+ "if not hf_token:\n",
719
+ " raise ValueError(\"HF_TOKEN not found in Google Colab secrets. Please ensure it is set.\")\n",
720
+ "\n",
721
+ "# Login to Hugging Face Hub\n",
722
+ "print(\"\\nLogging into Hugging Face...\")\n",
723
+ "login(token=hf_token)\n",
724
+ "\n",
725
+ "api = HfApi()\n",
726
+ "\n",
727
+ "# Define the target repository ID as specified by the user\n",
728
+ "repo_id = \"codeShare/FLUX.2-klein-9b-SDNQ-2bit\"\n",
729
+ "\n",
730
+ "# Check if repository exists, if not, create it\n",
731
+ "print(f\"Checking if repository {repo_id} exists...\")\n",
732
+ "try:\n",
733
+ " if not api.repo_exists(repo_id=repo_id, repo_type=\"model\"):\n",
734
+ " print(f\"Repository {repo_id} not found. Creating it...\")\n",
735
+ " create_repo(repo_id=repo_id, repo_type=\"model\", private=False, token=hf_token)\n",
736
+ " print(f\"Repository {repo_id} created successfully.\")\n",
737
+ " else:\n",
738
+ " print(f\"Repository {repo_id} already exists.\")\n",
739
+ "except HfHubHTTPError as e:\n",
740
+ " print(f\"An error occurred while checking or creating the repository: {e}\")\n",
741
+ " raise # Re-raise other HTTP errors\n",
742
+ "\n",
743
+ "# Define paths for the folders to upload\n",
744
+ "folders_to_upload = [\"/content/vae\", \"/content/tokenizer\", \"/content/scheduler\"]\n",
745
+ "\n",
746
+ "# Define the source path for model.safetensors.index.json and its target name in the repo\n",
747
+ "model_index_source_path = \"/content/text_encoder/model.safetensors.index.json\"\n",
748
+ "model_index_target_filename = \"model_index.json\"\n",
749
+ "\n",
750
+ "print(f\"\\nUploading specified components to {repo_id}...\")\n",
751
+ "\n",
752
+ "# Upload each specified folder\n",
753
+ "for folder_path in folders_to_upload:\n",
754
+ " if os.path.isdir(folder_path):\n",
755
+ " print(f\"Uploading folder: {folder_path}...\")\n",
756
+ " api.upload_folder(\n",
757
+ " folder_path=folder_path,\n",
758
+ " repo_id=repo_id,\n",
759
+ " repo_type=\"model\",\n",
760
+ " commit_message=f\"Upload {os.path.basename(folder_path)} component\",\n",
761
+ " )\n",
762
+ " print(f\"✅ Folder {folder_path} uploaded.\")\n",
763
+ " else:\n",
764
+ " print(f\"⚠️ Folder not found, skipping: {folder_path}\")\n",
765
+ "\n",
766
+ "# Upload model.safetensors.index.json as model_index.json\n",
767
+ "if os.path.exists(model_index_source_path):\n",
768
+ " print(f\"Uploading file: {model_index_source_path} as {model_index_target_filename}...\")\n",
769
+ " api.upload_file(\n",
770
+ " path_or_fileobj=model_index_source_path,\n",
771
+ " path_in_repo=model_index_target_filename,\n",
772
+ " repo_id=repo_id,\n",
773
+ " repo_type=\"model\",\n",
774
+ " commit_message=f\"Upload {model_index_target_filename}\",\n",
775
+ " )\n",
776
+ " print(f\"✅ File {model_index_source_path} uploaded as {model_index_target_filename}.\")\n",
777
+ "else:\n",
778
+ " print(f\"⚠️ {model_index_source_path} not found, skipping upload of {model_index_target_filename}.\")\n",
779
+ "\n",
780
+ "print(\"\\n✅ All specified components processed for upload to Hugging Face Hub.\")"
781
+ ],
782
+ "execution_count": null,
783
+ "outputs": []
784
+ },
785
+ {
786
+ "cell_type": "code",
787
+ "metadata": {
788
+ "id": "533c64b4"
789
+ },
790
+ "source": [
791
+ "import torch\n",
792
+ "import gc\n",
793
+ "import os\n",
794
+ "import shutil\n",
795
+ "from huggingface_hub import login\n",
796
+ "from diffusers import Flux2KleinPipeline, Flux2Transformer2DModel, AutoencoderKLFlux2\n",
797
+ "from transformers import Qwen2Tokenizer, Qwen3ForCausalLM\n",
798
+ "from sdnq import SDNQConfig, sdnq_post_load_quant # Ensure sdnq is imported\n",
799
+ "\n",
800
+ "# Define paths for local saved components\n",
801
+ "text_encoder_local_path = \"/content/text_encoder\"\n",
802
+ "transformer_local_path = \"/content/transformer\"\n",
803
+ "vae_local_path = \"/content/vae\"\n",
804
+ "\n",
805
+ "# Define the temporary directory to assemble the pipeline\n",
806
+ "rebuilt_pipeline_dir = \"/content/rebuilt_pipeline_temp\"\n",
807
+ "\n",
808
+ "print(f\"🔄 Rebuilding pipeline components in {rebuilt_pipeline_dir}...\")\n",
809
+ "\n",
810
+ "# Clean up and create the temporary directory\n",
811
+ "shutil.rmtree(rebuilt_pipeline_dir, ignore_errors=True)\n",
812
+ "os.makedirs(rebuilt_pipeline_dir, exist_ok=True)\n",
813
+ "\n",
814
+ "# Step 1: Load and save tokenizer and scheduler from the original model\n",
815
+ "# These components were not SDNQ'd or individually saved as models, so we fetch them from the source.\n",
816
+ "print(\"Loading original pipeline briefly to extract tokenizer and scheduler...\")\n",
817
+ "original_pipe_for_components = Flux2KleinPipeline.from_pretrained(\n",
818
+ " \"black-forest-labs/FLUX.2-klein-9B\",\n",
819
+ " torch_dtype=torch.bfloat16, # Consistent with initial load\n",
820
+ " low_cpu_mem_usage=True,\n",
821
+ " device_map=\"cpu\",\n",
822
+ ")\n",
823
+ "\n",
824
+ "original_pipe_for_components.tokenizer.save_pretrained(os.path.join(rebuilt_pipeline_dir, \"tokenizer\"))\n",
825
+ "original_pipe_for_components.scheduler.save_pretrained(os.path.join(rebuilt_pipeline_dir, \"scheduler\"))\n",
826
+ "\n",
827
+ "del original_pipe_for_components\n",
828
+ "gc.collect()\n",
829
+ "torch.cuda.empty_cache()\n",
830
+ "\n",
831
+ "\n",
832
+ "# Step 2: Copy the locally SDNQ-saved model components into the temporary pipeline directory\n",
833
+ "print(f\"Copying SDNQ-applied text_encoder, transformer, vae to {rebuilt_pipeline_dir}...\")\n",
834
+ "\n",
835
+ "# Ensure the subdirectories exist in the rebuilt_pipeline_dir before copying\n",
836
+ "os.makedirs(os.path.join(rebuilt_pipeline_dir, \"text_encoder\"), exist_ok=True)\n",
837
+ "os.makedirs(os.path.join(rebuilt_pipeline_dir, \"transformer\"), exist_ok=True)\n",
838
+ "os.makedirs(os.path.join(rebuilt_pipeline_dir, \"vae\"), exist_ok=True)\n",
839
+ "\n",
840
+ "# Copy contents of text_encoder_local_path into rebuilt_pipeline_dir/text_encoder\n",
841
+ "for item_name in os.listdir(text_encoder_local_path):\n",
842
+ " s = os.path.join(text_encoder_local_path, item_name)\n",
843
+ " d = os.path.join(rebuilt_pipeline_dir, \"text_encoder\", item_name)\n",
844
+ " if os.path.isdir(s):\n",
845
+ " shutil.copytree(s, d, dirs_exist_ok=True)\n",
846
+ " else:\n",
847
+ " shutil.copy2(s, d)\n",
848
+ "\n",
849
+ "# Copy contents of transformer_local_path into rebuilt_pipeline_dir/transformer\n",
850
+ "for item_name in os.listdir(transformer_local_path):\n",
851
+ " s = os.path.join(transformer_local_path, item_name)\n",
852
+ " d = os.path.join(rebuilt_pipeline_dir, \"transformer\", item_name)\n",
853
+ " if os.path.isdir(s):\n",
854
+ " shutil.copytree(s, d, dirs_exist_ok=True)\n",
855
+ " else:\n",
856
+ " shutil.copy2(s, d)\n",
857
+ "\n",
858
+ "# Copy contents of vae_local_path into rebuilt_pipeline_dir/vae\n",
859
+ "for item_name in os.listdir(vae_local_path):\n",
860
+ " s = os.path.join(vae_local_path, item_name)\n",
861
+ " d = os.path.join(rebuilt_pipeline_dir, \"vae\", item_name)\n",
862
+ " if os.path.isdir(s):\n",
863
+ " shutil.copytree(s, d, dirs_exist_ok=True)\n",
864
+ " else:\n",
865
+ " shutil.copy2(s, d)\n",
866
+ "\n",
867
+ "# Step 3: Load the full pipeline from the temporary directory\n",
868
+ "print(f\"Loading full pipeline from {rebuilt_pipeline_dir}...\")\n",
869
+ "new_pipe = Flux2KleinPipeline.from_pretrained(\n",
870
+ " rebuilt_pipeline_dir,\n",
871
+ " torch_dtype=torch.bfloat16, # Consistent with initial load\n",
872
+ " low_cpu_mem_usage=True,\n",
873
+ " device_map=\"cpu\", # Important for offloading\n",
874
+ ")\n",
875
+ "print(\"✅ Base pipeline rebuilt from local components.\")\n",
876
+ "\n",
877
+ "# Step 4: Re-apply SDNQ to the loaded components\n",
878
+ "# When loaded via from_pretrained, the base models are loaded, not the SDNQ wrappers.\n",
879
+ "print(\"🔥 Re-applying SDNQ optimizations to text_encoder, transformer, and vae...\")\n",
880
+ "sdnq_params = dict(\n",
881
+ " use_dynamic_quantization=True,\n",
882
+ " weights_dtype=\"uint4\",\n",
883
+ " dynamic_loss_threshold=1e-2,\n",
884
+ " use_svd=True, # Set to True for consistency with original SDNQ application\n",
885
+ " group_size=0,\n",
886
+ " quantization_device=\"cuda\", # Set to cuda for consistency with original SDNQ application\n",
887
+ " return_device=\"cpu\",\n",
888
+ " quant_conv=False,\n",
889
+ " quant_embedding=False,\n",
890
+ ")\n",
891
+ "\n",
892
+ "new_pipe.transformer = sdnq_post_load_quant(new_pipe.transformer, **sdnq_params)\n",
893
+ "new_pipe.text_encoder = sdnq_post_load_quant(new_pipe.text_encoder, **sdnq_params)\n",
894
+ "new_pipe.vae = sdnq_post_load_quant(new_pipe.vae, **sdnq_params)\n",
895
+ "print(\"✅ SDNQ re-applied to all necessary components.\")\n",
896
+ "\n",
897
+ "gc.collect()\n",
898
+ "torch.cuda.empty_cache()\n",
899
+ "\n",
900
+ "# Step 5: Login to Hugging Face and push the new pipeline\n",
901
+ "print(\"\\nLogging into Hugging Face...\")\n",
902
+ "login(token=userdata.get(\"HF_TOKEN\")) # hf_token is available from CELL 1\n",
903
+ "\n",
904
+ "print(\"\\nPushing rebuilt and SDNQ-applied pipeline to Hugging Face Hub...\")\n",
905
+ "new_pipe.push_to_hub(\n",
906
+ " repo_id=\"codeShare/FLUX.2-klein-9b-SDNQ-2bit\", # Using the 2bit repo_id as per request\n",
907
+ " safe_serialization=True,\n",
908
+ " commit_message=\"Rebuilt and pushed SDNQ Flux2 Klein 9b (2bit) after local save/crash\"\n",
909
+ ")\n",
910
+ "\n",
911
+ "print(\"\\n✅ Rebuilt pipeline pushed to Hugging Face Hub.\")\n",
912
+ "\n",
913
+ "# Update the global pipe object to the newly built one\n",
914
+ "pipe = new_pipe"
915
+ ],
916
+ "execution_count": null,
917
+ "outputs": []
918
+ },
919
+ {
920
+ "cell_type": "code",
921
+ "metadata": {
922
+ "id": "3bdfe2e2"
923
+ },
924
+ "source": [
925
+ "import os\n",
926
+ "from huggingface_hub import HfApi, login, create_repo\n",
927
+ "from google.colab import userdata\n",
928
+ "from huggingface_hub.utils import HfHubHTTPError\n",
929
+ "\n",
930
+ "# Retrieve hf_token from Colab secrets\n",
931
+ "hf_token = userdata.get(\"HF_TOKEN\")\n",
932
+ "if not hf_token:\n",
933
+ " raise ValueError(\"HF_TOKEN not found in Google Colab secrets. Please ensure it is set.\")\n",
934
+ "\n",
935
+ "# Login to Hugging Face Hub\n",
936
+ "print(\"\\nLogging into Hugging Face...\")\n",
937
+ "login(token=hf_token)\n",
938
+ "\n",
939
+ "api = HfApi()\n",
940
+ "\n",
941
+ "# Define the target repository ID\n",
942
+ "repo_id = \"codeShare/FLUX.2-klein-9b-SDNQ-2bit\" # Or \"codeShare/FLUX.2-klein-9b-SDNQ-4bit\" if that was the intended repo\n",
943
+ "\n",
944
+ "# Check if repository exists, if not, create it\n",
945
+ "print(f\"Checking if repository {repo_id} exists...\")\n",
946
+ "try:\n",
947
+ " if not api.repo_exists(repo_id=repo_id, repo_type=\"model\"):\n",
948
+ " print(f\"Repository {repo_id} not found. Creating it...\")\n",
949
+ " create_repo(repo_id=repo_id, repo_type=\"model\", private=False, token=hf_token)\n",
950
+ " print(f\"Repository {repo_id} created successfully.\")\n",
951
+ " else:\n",
952
+ " print(f\"Repository {repo_id} already exists.\")\n",
953
+ "except HfHubHTTPError as e:\n",
954
+ " print(f\"An error occurred while checking or creating the repository: {e}\")\n",
955
+ " raise # Re-raise other HTTP errors\n",
956
+ "\n",
957
+ "# Define local paths for the folders to upload\n",
958
+ "local_folders_to_upload = [\"/content/vae\", \"/content/tokenizer\", \"/content/scheduler\"]\n",
959
+ "\n",
960
+ "# Define the source path for model.safetensors.index.json and its target name in the repo\n",
961
+ "model_index_source_path = \"/content/text_encoder/model.safetensors.index.json\" # Assuming text_encoder still exists\n",
962
+ "model_index_target_filename = \"model_index.json\"\n",
963
+ "\n",
964
+ "print(f\"\\nUploading specified components to {repo_id}...\")\n",
965
+ "\n",
966
+ "# Upload each specified folder to a corresponding path in the repo\n",
967
+ "for local_folder_path in local_folders_to_upload:\n",
968
+ " folder_name = os.path.basename(local_folder_path)\n",
969
+ " if os.path.isdir(local_folder_path):\n",
970
+ " print(f\"Uploading folder: {local_folder_path} to repo path: {folder_name}...\")\n",
971
+ " api.upload_folder(\n",
972
+ " folder_path=local_folder_path,\n",
973
+ " repo_id=repo_id,\n",
974
+ " repo_type=\"model\",\n",
975
+ " path_in_repo=folder_name, # Uploads contents of local_folder_path into a folder named folder_name in the repo\n",
976
+ " commit_message=f\"Upload {folder_name} component\",\n",
977
+ " )\n",
978
+ " print(f\"✅ Folder {local_folder_path} uploaded to {repo_id}/{folder_name}.\")\n",
979
+ " else:\n",
980
+ " print(f\"⚠️ Local folder not found, skipping: {local_folder_path}\")\n",
981
+ "\n",
982
+ "# Upload model.safetensors.index.json as model_index.json to the root of the repo\n",
983
+ "if os.path.exists(model_index_source_path):\n",
984
+ " print(f\"Uploading file: {model_index_source_path} as {model_index_target_filename} to the repo root...\")\n",
985
+ " api.upload_file(\n",
986
+ " path_or_fileobj=model_index_source_path,\n",
987
+ " path_in_repo=model_index_target_filename,\n",
988
+ " repo_id=repo_id,\n",
989
+ " repo_type=\"model\",\n",
990
+ " commit_message=f\"Upload {model_index_target_filename}\",\n",
991
+ " )\n",
992
+ " print(f\"✅ File {model_index_source_path} uploaded.\")\n",
993
+ "else:\n",
994
+ " print(f\"⚠️ {model_index_source_path} not found, skipping upload of {model_index_target_filename}.\")\n",
995
+ "\n",
996
+ "print(\"\\n✅ All specified components processed for upload to Hugging Face Hub.\")"
997
+ ],
998
+ "execution_count": null,
999
+ "outputs": []
1000
+ },
1001
+ {
1002
+ "cell_type": "code",
1003
+ "metadata": {
1004
+ "id": "a36d8446"
1005
+ },
1006
+ "source": [
1007
+ "import os\n",
1008
+ "from huggingface_hub import HfApi, login, create_repo\n",
1009
+ "from google.colab import userdata\n",
1010
+ "from huggingface_hub.utils import HfHubHTTPError\n",
1011
+ "\n",
1012
+ "# Retrieve hf_token from Colab secrets\n",
1013
+ "hf_token = userdata.get(\"HF_TOKEN\")\n",
1014
+ "if not hf_token:\n",
1015
+ " raise ValueError(\"HF_TOKEN not found in Google Colab secrets. Please ensure it is set.\")\n",
1016
+ "\n",
1017
+ "# Login to Hugging Face Hub\n",
1018
+ "print(\"\\nLogging into Hugging Face...\")\n",
1019
+ "login(token=hf_token)\n",
1020
+ "\n",
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",
1029
+ " if not api.repo_exists(repo_id=repo_id, repo_type=\"model\"):\n",
1030
+ " print(f\"Repository {repo_id} not found. Creating it...\")\n",
1031
+ " create_repo(repo_id=repo_id, repo_type=\"model\", private=False, token=hf_token)\n",
1032
+ " print(f\"Repository {repo_id} created successfully.\")\n",
1033
+ " else:\n",
1034
+ " print(f\"Repository {repo_id} already exists.\")\n",
1035
+ "except HfHubHTTPError as e:\n",
1036
+ " print(f\"An error occurred while checking or creating the repository: {e}\")\n",
1037
+ " raise # Re-raise other HTTP errors\n",
1038
+ "\n",
1039
+ "# Define local paths for the folders to upload\n",
1040
+ "local_folders_to_upload = [\"/content/vae\", \"/content/tokenizer\", \"/content/scheduler\"]\n",
1041
+ "\n",
1042
+ "# Define the source path for model.safetensors.index.json and its target name in the repo\n",
1043
+ "model_index_source_path = \"/content/text_encoder/model.safetensors.index.json\" # Assuming text_encoder still exists\n",
1044
+ "model_index_target_filename = \"model_index.json\"\n",
1045
+ "\n",
1046
+ "print(f\"\\nUploading specified components to {repo_id}...\")\n",
1047
+ "\n",
1048
+ "# Upload each specified folder to a corresponding path in the repo\n",
1049
+ "for local_folder_path in local_folders_to_upload:\n",
1050
+ " folder_name = os.path.basename(local_folder_path)\n",
1051
+ " if os.path.isdir(local_folder_path):\n",
1052
+ " print(f\"Uploading folder: {local_folder_path} to repo path: {folder_name}...\")\n",
1053
+ " api.upload_folder(\n",
1054
+ " folder_path=local_folder_path,\n",
1055
+ " repo_id=repo_id,\n",
1056
+ " repo_type=\"model\",\n",
1057
+ " path_in_repo=folder_name, # Uploads contents of local_folder_path into a folder named folder_name in the repo\n",
1058
+ " commit_message=f\"Upload {folder_name} component\",\n",
1059
+ " )\n",
1060
+ " print(f\"✅ Folder {local_folder_path} uploaded to {repo_id}/{folder_name}.\")\n",
1061
+ " else:\n",
1062
+ " print(f\"⚠️ Local folder not found, skipping: {local_folder_path}\")\n",
1063
+ "\n",
1064
+ "# Upload model.safetensors.index.json as model_index.json to the root of the repo\n",
1065
+ "if os.path.exists(model_index_source_path):\n",
1066
+ " print(f\"Uploading file: {model_index_source_path} as {model_index_target_filename} to the repo root...\")\n",
1067
+ " api.upload_file(\n",
1068
+ " path_or_fileobj=model_index_source_path,\n",
1069
+ " path_in_repo=model_index_target_filename,\n",
1070
+ " repo_id=repo_id,\n",
1071
+ " repo_type=\"model\",\n",
1072
+ " commit_message=f\"Upload {model_index_target_filename}\",\n",
1073
+ " )\n",
1074
+ " print(f\"✅ File {model_index_source_path} uploaded as {repo_id}/{model_index_target_filename}.\")\n",
1075
+ "else:\n",
1076
+ " print(f\"⚠️ {model_index_source_path} not found, skipping upload of {model_index_target_filename}.\")\n",
1077
+ "\n",
1078
+ "print(\"\\n✅ All specified components processed for upload to Hugging Face Hub.\")"
1079
+ ],
1080
+ "execution_count": null,
1081
+ "outputs": []
1082
+ }
1083
+ ],
1084
+ "metadata": {
1085
+ "colab": {
1086
+ "provenance": [],
1087
+ "gpuType": "T4"
1088
+ },
1089
+ "kernelspec": {
1090
+ "display_name": "Python 3",
1091
+ "name": "python3"
1092
+ },
1093
+ "language_info": {
1094
+ "name": "python"
1095
+ },
1096
+ "accelerator": "GPU"
1097
+ },
1098
+ "nbformat": 4,
1099
+ "nbformat_minor": 0
1100
+ }