readme.md 11.9 KB
Newer Older
1
# Model Conversion Tool
2

gushiqiao's avatar
gushiqiao committed
3
A powerful model weight conversion tool that supports format conversion, quantization, LoRA merging, and more.
4

gushiqiao's avatar
gushiqiao committed
5
## Main Features
6

gushiqiao's avatar
gushiqiao committed
7
8
9
10
11
12
13
- **Format Conversion**: Support PyTorch (.pth) and SafeTensors (.safetensors) format conversion
- **Model Quantization**: Support INT8 and FP8 quantization to significantly reduce model size
- **Architecture Conversion**: Support conversion between LightX2V and Diffusers architectures
- **LoRA Merging**: Support loading and merging multiple LoRA formats
- **Multi-Model Support**: Support Wan DiT, Qwen Image DiT, T5, CLIP, etc.
- **Flexible Saving**: Support single file, block-based, and chunked saving methods
- **Parallel Processing**: Support parallel acceleration for large model conversion
14

gushiqiao's avatar
gushiqiao committed
15
## Supported Model Types
16

Yang Yong (雍洋)'s avatar
Yang Yong (雍洋) committed
17
- `hunyuan_dit`: hunyuan DiT 1.5 models
gushiqiao's avatar
gushiqiao committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
- `wan_dit`: Wan DiT series models (default)
- `wan_animate_dit`: Wan Animate DiT models
- `qwen_image_dit`: Qwen Image DiT models
- `wan_t5`: Wan T5 text encoder
- `wan_clip`: Wan CLIP vision encoder

## Core Parameters

### Basic Parameters

- `-s, --source`: Input path (file or directory)
- `-o, --output`: Output directory path
- `-o_e, --output_ext`: Output format, `.pth` or `.safetensors` (default)
- `-o_n, --output_name`: Output file name (default: `converted`)
- `-t, --model_type`: Model type (default: `wan_dit`)

### Architecture Conversion Parameters

- `-d, --direction`: Conversion direction
  - `None`: No architecture conversion (default)
  - `forward`: LightX2V → Diffusers
  - `backward`: Diffusers → LightX2V

### Quantization Parameters

- `--quantized`: Enable quantization
- `--bits`: Quantization bit width, currently only supports 8-bit
- `--linear_dtype`: Linear layer quantization type
  - `torch.int8`: INT8 quantization
  - `torch.float8_e4m3fn`: FP8 quantization
- `--non_linear_dtype`: Non-linear layer data type
  - `torch.bfloat16`: BF16
  - `torch.float16`: FP16
  - `torch.float32`: FP32 (default)
52
- `--device`: Device for quantization, `cpu` or `cuda` (default)
gushiqiao's avatar
gushiqiao committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
- `--comfyui_mode`: ComfyUI compatible mode
- `--full_quantized`: Full quantization mode (effective in ComfyUI mode)

### LoRA Parameters

- `--lora_path`: LoRA file path(s), supports multiple (separated by spaces)
- `--lora_strength`: LoRA strength coefficients, supports multiple (default: 1.0)
- `--alpha`: LoRA alpha parameters, supports multiple
- `--lora_key_convert`: LoRA key conversion mode
  - `auto`: Auto-detect (default)
  - `same`: Use original key names
  - `convert`: Apply same conversion as model

### Saving Parameters

- `--single_file`: Save as single file (note: large models consume significant memory)
- `-b, --save_by_block`: Save by blocks (recommended for backward conversion)
- `-c, --chunk-size`: Chunk size (default: 100, 0 means no chunking)
- `--copy_no_weight_files`: Copy non-weight files from source directory

### Performance Parameters

- `--parallel`: Enable parallel processing (default: True)
- `--no-parallel`: Disable parallel processing

## Supported LoRA Formats

The tool automatically detects and supports the following LoRA formats:

1. **Standard**: `{key}.lora_up.weight` and `{key}.lora_down.weight`
2. **Diffusers**: `{key}_lora.up.weight` and `{key}_lora.down.weight`
3. **Diffusers V2**: `{key}.lora_B.weight` and `{key}.lora_A.weight`
4. **Diffusers V3**: `{key}.lora.up.weight` and `{key}.lora.down.weight`
5. **Mochi**: `{key}.lora_B` and `{key}.lora_A` (no .weight suffix)
6. **Transformers**: `{key}.lora_linear_layer.up.weight` and `{key}.lora_linear_layer.down.weight`
7. **Qwen**: `{key}.lora_B.default.weight` and `{key}.lora_A.default.weight`

Additionally supports diff formats:
- `.diff`: Weight diff
- `.diff_b`: Bias diff
- `.diff_m`: Modulation diff

## Usage Examples

### 1. Model Quantization

#### 1.1 Wan DiT Quantization to INT8

**Multiple safetensors, saved by dit blocks**
102
103
```bash
python converter.py \
gushiqiao's avatar
gushiqiao committed
104
105
    --source /path/to/Wan2.1-I2V-14B-480P/ \
    --output /path/to/output \
gushiqiao's avatar
Fix  
gushiqiao committed
106
    --output_ext .safetensors \
107
    --output_name wan_int8 \
gushiqiao's avatar
gushiqiao committed
108
    --linear_dtype torch.int8 \
gushiqiao's avatar
gushiqiao committed
109
110
111
    --model_type wan_dit \
    --quantized \
    --save_by_block
112
113
```

gushiqiao's avatar
gushiqiao committed
114
**Single safetensor file**
115
116
```bash
python converter.py \
gushiqiao's avatar
gushiqiao committed
117
118
    --source /path/to/Wan2.1-I2V-14B-480P/ \
    --output /path/to/output \
gushiqiao's avatar
Fix  
gushiqiao committed
119
    --output_ext .safetensors \
gushiqiao's avatar
gushiqiao committed
120
121
    --output_name wan2.1_i2v_480p_int8_lightx2v \
    --linear_dtype torch.int8 \
gushiqiao's avatar
gushiqiao committed
122
123
    --model_type wan_dit \
    --quantized \
gushiqiao's avatar
gushiqiao committed
124
    --single_file
125
126
```

gushiqiao's avatar
gushiqiao committed
127
#### 1.2 Wan DiT Quantization to FP8
GoatWu's avatar
GoatWu committed
128

gushiqiao's avatar
gushiqiao committed
129
**Multiple safetensors, saved by dit blocks**
GoatWu's avatar
GoatWu committed
130
131
```bash
python converter.py \
gushiqiao's avatar
gushiqiao committed
132
133
    --source /path/to/Wan2.1-I2V-14B-480P/ \
    --output /path/to/output \
GoatWu's avatar
GoatWu committed
134
    --output_ext .safetensors \
gushiqiao's avatar
gushiqiao committed
135
136
137
    --output_name wan_fp8 \
    --linear_dtype torch.float8_e4m3fn \
    --non_linear_dtype torch.bfloat16 \
GoatWu's avatar
GoatWu committed
138
    --model_type wan_dit \
gushiqiao's avatar
gushiqiao committed
139
140
    --quantized \
    --save_by_block
GoatWu's avatar
GoatWu committed
141
142
```

gushiqiao's avatar
gushiqiao committed
143
**Single safetensor file**
144
145
```bash
python converter.py \
gushiqiao's avatar
gushiqiao committed
146
147
    --source /path/to/Wan2.1-I2V-14B-480P/ \
    --output /path/to/output \
gushiqiao's avatar
Fix  
gushiqiao committed
148
    --output_ext .safetensors \
gushiqiao's avatar
gushiqiao committed
149
    --output_name wan2.1_i2v_480p_scaled_fp8_e4m3_lightx2v \
gushiqiao's avatar
gushiqiao committed
150
    --linear_dtype torch.float8_e4m3fn \
gushiqiao's avatar
gushiqiao committed
151
152
153
154
    --non_linear_dtype torch.bfloat16 \
    --model_type wan_dit \
    --quantized \
    --single_file
155
156
```

gushiqiao's avatar
gushiqiao committed
157
**ComfyUI scaled_fp8 format**
158
159
```bash
python converter.py \
gushiqiao's avatar
gushiqiao committed
160
161
    --source /path/to/Wan2.1-I2V-14B-480P/ \
    --output /path/to/output \
162
    --output_ext .safetensors \
gushiqiao's avatar
gushiqiao committed
163
164
165
166
    --output_name wan2.1_i2v_480p_scaled_fp8_e4m3_lightx2v_comfyui \
    --linear_dtype torch.float8_e4m3fn \
    --non_linear_dtype torch.bfloat16 \
    --model_type wan_dit \
167
    --quantized \
gushiqiao's avatar
gushiqiao committed
168
169
    --single_file \
    --comfyui_mode
170
171
```

gushiqiao's avatar
gushiqiao committed
172
**ComfyUI full FP8 format**
173
174
```bash
python converter.py \
gushiqiao's avatar
gushiqiao committed
175
176
    --source /path/to/Wan2.1-I2V-14B-480P/ \
    --output /path/to/output \
177
    --output_ext .safetensors \
gushiqiao's avatar
gushiqiao committed
178
    --output_name wan2.1_i2v_480p_scaled_fp8_e4m3_lightx2v_comfyui \
179
    --linear_dtype torch.float8_e4m3fn \
gushiqiao's avatar
gushiqiao committed
180
181
    --non_linear_dtype torch.bfloat16 \
    --model_type wan_dit \
182
    --quantized \
gushiqiao's avatar
gushiqiao committed
183
184
185
    --single_file \
    --comfyui_mode \
    --full_quantized
186
```
187

gushiqiao's avatar
gushiqiao committed
188
> **Tip**: For other DIT models, simply switch the `--model_type` parameter
189

gushiqiao's avatar
gushiqiao committed
190
191
192
#### 1.3 T5 Encoder Quantization

**INT8 Quantization**
193
194
```bash
python converter.py \
gushiqiao's avatar
gushiqiao committed
195
196
197
    --source /path/to/models_t5_umt5-xxl-enc-bf16.pth \
    --output /path/to/output \
    --output_ext .pth \
198
    --output_name models_t5_umt5-xxl-enc-int8 \
gushiqiao's avatar
gushiqiao committed
199
200
    --linear_dtype torch.int8 \
    --non_linear_dtype torch.bfloat16 \
gushiqiao's avatar
gushiqiao committed
201
202
    --model_type wan_t5 \
    --quantized
203
204
```

gushiqiao's avatar
gushiqiao committed
205
**FP8 Quantization**
206
207
```bash
python converter.py \
gushiqiao's avatar
gushiqiao committed
208
209
210
    --source /path/to/models_t5_umt5-xxl-enc-bf16.pth \
    --output /path/to/output \
    --output_ext .pth \
211
    --output_name models_t5_umt5-xxl-enc-fp8 \
gushiqiao's avatar
gushiqiao committed
212
213
    --linear_dtype torch.float8_e4m3fn \
    --non_linear_dtype torch.bfloat16 \
gushiqiao's avatar
gushiqiao committed
214
215
    --model_type wan_t5 \
    --quantized
216
217
```

gushiqiao's avatar
gushiqiao committed
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#### 1.4 CLIP Encoder Quantization

**INT8 Quantization**
```bash
python converter.py \
    --source /path/to/models_clip_open-clip-xlm-roberta-large-vit-huge-14.pth \
    --output /path/to/output \
    --output_ext .pth \
    --output_name models_clip_open-clip-xlm-roberta-large-vit-huge-14-int8 \
    --linear_dtype torch.int8 \
    --non_linear_dtype torch.float16 \
    --model_type wan_clip \
    --quantized
```

**FP8 Quantization**
```bash
python converter.py \
    --source /path/to/models_clip_open-clip-xlm-roberta-large-vit-huge-14.pth \
    --output /path/to/output \
    --output_ext .pth \
    --output_name models_clip_open-clip-xlm-roberta-large-vit-huge-14-fp8 \
    --linear_dtype torch.float8_e4m3fn \
    --non_linear_dtype torch.float16 \
    --model_type wan_clip \
    --quantized
```

Yang Yong (雍洋)'s avatar
Yang Yong (雍洋) committed
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277


#### 1.5 Qwen25_vl llm Quantization

**INT8 Quantization**
```bash
python converter.py \
    --source /path/to/hunyuanvideo-1.5/text_encoder/llm \
    --output /path/to/output \
    --output_ext .safetensors \
    --output_name qwen25vl-llm-int8 \
    --linear_dtype torch.int8 \
    --non_linear_dtype torch.float16 \
    --model_type qwen25vl_llm \
    --quantized \
    --single_file
```

**FP8 Quantization**
```bash
python converter.py \
    --source /path/to/hunyuanvideo-1.5/text_encoder/llm \
    --output /path/to/output \
    --output_ext .safetensors \
    --output_name qwen25vl-llm-fp8 \
    --linear_dtype torch.float8_e4m3fn \
    --non_linear_dtype torch.float16 \
    --model_type qwen25vl_llm \
    --quantized \
    --single_file
```

gushiqiao's avatar
gushiqiao committed
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
### 2. LoRA Merging

#### 2.1 Merge Single LoRA

```bash
python converter.py \
    --source /path/to/base_model/ \
    --output /path/to/output \
    --output_ext .safetensors \
    --output_name merged_model \
    --model_type wan_dit \
    --lora_path /path/to/lora.safetensors \
    --lora_strength 1.0 \
    --single_file
```
293

gushiqiao's avatar
gushiqiao committed
294
#### 2.2 Merge Multiple LoRAs
295
296
297

```bash
python converter.py \
gushiqiao's avatar
gushiqiao committed
298
299
300
301
302
303
304
305
306
307
308
    --source /path/to/base_model/ \
    --output /path/to/output \
    --output_ext .safetensors \
    --output_name merged_model \
    --model_type wan_dit \
    --lora_path /path/to/lora1.safetensors /path/to/lora2.safetensors \
    --lora_strength 1.0 0.8 \
    --single_file
```

#### 2.3 LoRA Merging with Quantization
309

gushiqiao's avatar
gushiqiao committed
310
311
312
313
314
315
316
317
318
319
320
321
322
**LoRA Merge → FP8 Quantization**
```bash
python converter.py \
    --source /path/to/base_model/ \
    --output /path/to/output \
    --output_ext .safetensors \
    --output_name merged_quantized \
    --model_type wan_dit \
    --lora_path /path/to/lora.safetensors \
    --lora_strength 1.0 \
    --quantized \
    --linear_dtype torch.float8_e4m3fn \
    --single_file
323
```
gushiqiao's avatar
gushiqiao committed
324
325

**LoRA Merge → ComfyUI scaled_fp8**
326
327
```bash
python converter.py \
gushiqiao's avatar
gushiqiao committed
328
329
330
331
332
333
334
335
336
337
338
    --source /path/to/base_model/ \
    --output /path/to/output \
    --output_ext .safetensors \
    --output_name merged_quantized \
    --model_type wan_dit \
    --lora_path /path/to/lora.safetensors \
    --lora_strength 1.0 \
    --quantized \
    --linear_dtype torch.float8_e4m3fn \
    --single_file \
    --comfyui_mode
339
```
340

gushiqiao's avatar
gushiqiao committed
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
**LoRA Merge → ComfyUI Full FP8**
```bash
python converter.py \
    --source /path/to/base_model/ \
    --output /path/to/output \
    --output_ext .safetensors \
    --output_name merged_quantized \
    --model_type wan_dit \
    --lora_path /path/to/lora.safetensors \
    --lora_strength 1.0 \
    --quantized \
    --linear_dtype torch.float8_e4m3fn \
    --single_file \
    --comfyui_mode \
    --full_quantized
```
357

gushiqiao's avatar
gushiqiao committed
358
#### 2.4 LoRA Key Conversion Modes
359

gushiqiao's avatar
gushiqiao committed
360
**Auto-detect mode (recommended)**
361
362
```bash
python converter.py \
gushiqiao's avatar
gushiqiao committed
363
364
365
366
367
    --source /path/to/model/ \
    --output /path/to/output \
    --lora_path /path/to/lora.safetensors \
    --lora_key_convert auto \
    --single_file
368
369
```

gushiqiao's avatar
gushiqiao committed
370
**Use original key names (LoRA already in target format)**
371
372
```bash
python converter.py \
gushiqiao's avatar
gushiqiao committed
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
    --source /path/to/model/ \
    --output /path/to/output \
    --direction forward \
    --lora_path /path/to/lora.safetensors \
    --lora_key_convert same \
    --single_file
```

**Apply conversion (LoRA in source format)**
```bash
python converter.py \
    --source /path/to/model/ \
    --output /path/to/output \
    --direction forward \
    --lora_path /path/to/lora.safetensors \
    --lora_key_convert convert \
    --single_file
```

### 3. Architecture Format Conversion

#### 3.1 LightX2V → Diffusers

```bash
python converter.py \
    --source /path/to/Wan2.1-I2V-14B-480P \
    --output /path/to/Wan2.1-I2V-14B-480P-Diffusers \
    --output_ext .safetensors \
    --model_type wan_dit \
    --direction forward \
    --chunk-size 100
```

#### 3.2 Diffusers → LightX2V

```bash
python converter.py \
    --source /path/to/Wan2.1-I2V-14B-480P-Diffusers \
    --output /path/to/Wan2.1-I2V-14B-480P \
    --output_ext .safetensors \
    --model_type wan_dit \
    --direction backward \
    --save_by_block
```

### 4. Format Conversion

#### 4.1 .pth → .safetensors

```bash
python converter.py \
    --source /path/to/model.pth \
    --output /path/to/output \
    --output_ext .safetensors \
427
428
    --output_name model \
    --single_file
gushiqiao's avatar
gushiqiao committed
429
430
431
432
433
434
435
436
437
438
439
```

#### 4.2 Multiple .safetensors → Single File

```bash
python converter.py \
    --source /path/to/model_directory/ \
    --output /path/to/output \
    --output_ext .safetensors \
    --output_name merged_model \
    --single_file
440
```