kelseye commited on
Commit
363fdc8
·
verified ·
1 Parent(s): 211f492

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -53,8 +53,7 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
53
  "/audio_vae_nf4.safetensors" filter=lfs diff=lfs merge=lfs -text
54
  "/minimax-h3-fl2va-nf4.safetensors" filter=lfs diff=lfs merge=lfs -text
55
  "/minimax-h3-ref2va-nf4.safetensors" filter=lfs diff=lfs merge=lfs -text
56
- "/minimax-h3-text-encoder-nf4.safetensors" filter=lfs diff=lfs merge=lfs -textaudio_vae_nf4.safetensors filter=lfs diff=lfs merge=lfs -text
57
- minimax-h3-fl2va-nf4.safetensors filter=lfs diff=lfs merge=lfs -text
58
- minimax-h3-ref2va-nf4.safetensors filter=lfs diff=lfs merge=lfs -text
59
- minimax-h3-text-encoder-nf4.safetensors filter=lfs diff=lfs merge=lfs -text
60
- video_vae_nf4.safetensors filter=lfs diff=lfs merge=lfs -text
 
53
  "/audio_vae_nf4.safetensors" filter=lfs diff=lfs merge=lfs -text
54
  "/minimax-h3-fl2va-nf4.safetensors" filter=lfs diff=lfs merge=lfs -text
55
  "/minimax-h3-ref2va-nf4.safetensors" filter=lfs diff=lfs merge=lfs -text
56
+ "/minimax-h3-text-encoder-nf4.safetensors" filter=lfs diff=lfs merge=lfs -text
57
+ "/minimax-h3-fl2va-pruned-nf4.safetensors" filter=lfs diff=lfs merge=lfs -text
58
+ "/minimax-h3-ref2va-pruned-nf4.safetensors" filter=lfs diff=lfs merge=lfs -textminimax-h3-fl2va-pruned-nf4.safetensors filter=lfs diff=lfs merge=lfs -text
59
+ minimax-h3-ref2va-pruned-nf4.safetensors filter=lfs diff=lfs merge=lfs -text
 
README.md CHANGED
@@ -1,15 +1,9 @@
1
  ---
2
  license: apache-2.0
3
- base_model:
4
- - MiniMaxAI/MiniMax-H3
5
- frameworks:
6
- - ""
7
- base_model_relation: quantized
8
  ---
9
-
10
  # MiniMax-H3-NF4
11
 
12
- This model is the **NF4 quantized version** of the video generation model [MiniMax-H3](https://modelscope.cn/models/MiniMax/MiniMax-H3). It utilizes the `bitsandbytes` 4-bit quantization scheme and is designed to be used with [DiffSynth-Studio](https://github.com/modelscope/DiffSynth-Studio), enabling model inference on devices with limited VRAM and RAM.
13
 
14
  ## Environment Setup
15
 
@@ -21,11 +15,11 @@ pip install -e ".[all]"
21
 
22
  ## Inference Code
23
 
24
- ### Enable VRAM Management
25
 
26
- Run the following code to perform inference using DiffSynth-Studio. VRAM management will be automatically enabled. The actual VRAM usage depends on the available VRAM on your GPU; a minimum of 8GB VRAM is required to run.
27
 
28
- #### FL2VA (Text-to-Video/Audio):
29
 
30
  ```python
31
  import torch
@@ -44,6 +38,7 @@ vram_config = {
44
  "computation_dtype": torch.bfloat16,
45
  "computation_device": "cuda",
46
  }
 
47
  pipe = MiniMaxH3Pipeline.from_pretrained(
48
  torch_dtype=torch.bfloat16,
49
  device="cuda",
@@ -68,11 +63,11 @@ write_video_audio(
68
  )
69
  ```
70
 
71
- #### Ref2VA (Reference-to-Video/Audio):
72
 
73
  <details>
74
 
75
- <summary>Expand Code</summary>
76
 
77
  ```python
78
  import torch
@@ -127,7 +122,7 @@ pipe = MiniMaxH3Pipeline.from_pretrained(
127
  # Text + Reference Image -> Video + Audio
128
  dataset_snapshot_download(dataset_id="DiffSynth-Studio/diffsynth_example_dataset", local_dir="data/diffsynth_example_dataset", allow_file_pattern="minimax_h3/MiniMax-H3-Ref2VA/*")
129
  ref_image = Image.open("data/diffsynth_example_dataset/minimax_h3/MiniMax-H3-Ref2VA/0.png").convert("RGB")
130
- prompt = "A website page, website UI design, website animation, video showing smooth webpage scrolling effect. A highly explosive and dynamic product official website style product landing page UI/UX demo video, the core display subject is product image 1. The page uses bold, powerful, tilted oversized sans-serif fonts for flamboyant typography. The background features dynamic light and shadow with extreme speed sense, dark carbon fiber or sports breathable mesh textures interweaving and changing. The video shows a tight-paced, powerful webpage downward scrolling effect, as well as strong visual zoom and color inversion UI interaction actions when hovering the mouse."
131
  video, audio = pipe(
132
  prompt=prompt,
133
  height=480, width=832, num_frames=124, num_inference_steps=50, seed=42,
@@ -158,9 +153,152 @@ write_video_audio(
158
 
159
  </details>
160
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
  ### Extreme Hardware Optimization
162
 
163
- If your computing device has extremely limited performance, we support enabling direct disk-to-VRAM loading. With this configuration, tensors in the model are loaded from disk to VRAM one by one according to the computation order. This allows the model to run with only 8GB of RAM:
164
 
165
  ```diff
166
  vram_config = {
@@ -182,7 +320,7 @@ pipe = MiniMaxH3Pipeline.from_pretrained(
182
  )
183
  ```
184
 
185
- We also support running model inference on Mac M-series chips, although this is not recommended:
186
 
187
  ```diff
188
  vram_config = {
@@ -206,15 +344,15 @@ pipe = MiniMaxH3Pipeline.from_pretrained(
206
 
207
  ## Training Code
208
 
209
- This quantized model supports LoRA training. Please follow the steps below to start the training program.
210
 
211
- Download the sample dataset:
212
 
213
  ```shell
214
  modelscope download --dataset DiffSynth-Studio/diffsynth_example_dataset --include "minimax_h3/MiniMax-H3-FL2VA/*" --local_dir ./data/diffsynth_example_dataset
215
  ```
216
 
217
- **Training configuration suitable for Data Center GPUs (e.g., Nvidia H20):** Run the following script to start the LoRA training program. Requires 48GB VRAM.
218
 
219
  ```shell
220
  accelerate launch examples/minimax_h3/model_training/train.py \
@@ -238,7 +376,7 @@ accelerate launch examples/minimax_h3/model_training/train.py \
238
  --find_unused_parameters
239
  ```
240
 
241
- **Training configuration suitable for Consumer GPUs (e.g., Nvidia RTX 4090):** Run the following scripts to start two-stage split training with gradient checkpointing offload. Requires 24GB VRAM.
242
 
243
  ```shell
244
  accelerate launch examples/minimax_h3/model_training/train.py \
@@ -289,4 +427,4 @@ accelerate launch examples/minimax_h3/model_training/train.py \
289
  * DiffSynth-Studio Documentation: [Minimax-H3](https://diffsynth-studio-doc.readthedocs.io/en/latest/Model_Details/MiniMax-H3.html)
290
  * DiffSynth-Studio Documentation: [VRAM Management](https://diffsynth-studio-doc.readthedocs.io/en/latest/Pipeline_Usage/VRAM_management.html)
291
  * DiffSynth-Studio Documentation: [Two-Stage Split Training](https://diffsynth-studio-doc.readthedocs.io/en/latest/Training/Split_Training.html)
292
- * DiffSynth-Studio Documentation: [Low VRAM Training](https://diffsynth-studio-doc.readthedocs.io/en/latest/Pipeline_Usage/Model_Training.html#low-vram-training)
 
1
  ---
2
  license: apache-2.0
 
 
 
 
 
3
  ---
 
4
  # MiniMax-H3-NF4
5
 
6
+ This model is the **NF4 quantized version** of the video generation model [MiniMax-H3](https://modelscope.cn/models/MiniMax/MiniMax-H3), using the `bitsandbytes` 4-bit quantization method. When used with [DiffSynth-Studio](https://github.com/modelscope/DiffSynth-Studio), it enables model inference on devices with limited GPU memory and system RAM.
7
 
8
  ## Environment Setup
9
 
 
15
 
16
  ## Inference Code
17
 
18
+ ### Enable Memory Management
19
 
20
+ Run the following code to perform inference using DiffSynth-Studio. Memory management will be automatically enabled. The actual GPU memory usage depends on available GPU memory, and the model can run with as little as 8GB of GPU memory.
21
 
22
+ FL2VA:
23
 
24
  ```python
25
  import torch
 
38
  "computation_dtype": torch.bfloat16,
39
  "computation_device": "cuda",
40
  }
41
+
42
  pipe = MiniMaxH3Pipeline.from_pretrained(
43
  torch_dtype=torch.bfloat16,
44
  device="cuda",
 
63
  )
64
  ```
65
 
66
+ Ref2VA:
67
 
68
  <details>
69
 
70
+ <summary>Expand code</summary>
71
 
72
  ```python
73
  import torch
 
122
  # Text + Reference Image -> Video + Audio
123
  dataset_snapshot_download(dataset_id="DiffSynth-Studio/diffsynth_example_dataset", local_dir="data/diffsynth_example_dataset", allow_file_pattern="minimax_h3/MiniMax-H3-Ref2VA/*")
124
  ref_image = Image.open("data/diffsynth_example_dataset/minimax_h3/MiniMax-H3-Ref2VA/0.png").convert("RGB")
125
+ prompt = "A webpage, website UI design, web animation. The video demonstrates a smooth scrolling-down effect. A highly dynamic and energetic product homepage-style landing page UI/UX demo video, with the main focus being product image 1. The page features bold, slanted, oversized sans-serif typography for striking layout. The background contains fast-moving light effects combined with dynamically shifting dark carbon fiber or sport breathable mesh textures. The video shows a tightly paced, powerful scrolling-down animation, along with strong UI interactions such as visual zoom-in and color inversion on mouse hover."
126
  video, audio = pipe(
127
  prompt=prompt,
128
  height=480, width=832, num_frames=124, num_inference_steps=50, seed=42,
 
153
 
154
  </details>
155
 
156
+ [Comfy-Org/MiniMax-H3](https://www.modelscope.cn/models/Comfy-Org/MiniMax-H3) provides a pruned variant of MiniMax-H3, which replaces the DiT's timestep embedding MLP with a lookup table, reducing the input dimension of `adaln_proj.linear` from 2688 to 8 and bringing the total number of model parameters down to approximately 20B. We also provide NF4 quantized weights for this version, with each DiT file being around 9.8 GB.
157
+
158
+ Usage is identical to the above; simply replace the DiT's `origin_file_pattern` with the pruned version. The text encoder and both VAEs still reuse the same set of weights.
159
+
160
+ FL2VA:
161
+
162
+ <details>
163
+
164
+ <summary>Expand code</summary>
165
+
166
+ ```python
167
+ import torch
168
+ from diffsynth.pipelines.minimax_h3_audio_video import MiniMaxH3Pipeline, ModelConfig
169
+ from diffsynth.utils.data.audio_video import write_video_audio
170
+ from modelscope import dataset_snapshot_download
171
+ from PIL import Image
172
+
173
+ vram_config = {
174
+ "offload_dtype": "disk",
175
+ "offload_device": "disk",
176
+ "onload_dtype": torch.bfloat16,
177
+ "onload_device": "cpu",
178
+ "preparing_dtype": torch.bfloat16,
179
+ "preparing_device": "cuda",
180
+ "computation_dtype": torch.bfloat16,
181
+ "computation_device": "cuda",
182
+ }
183
+ pipe = MiniMaxH3Pipeline.from_pretrained(
184
+ torch_dtype=torch.bfloat16,
185
+ device="cuda",
186
+ model_configs=[
187
+ ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="minimax-h3-fl2va-pruned-nf4.safetensors", **vram_config),
188
+ ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="minimax-h3-text-encoder-nf4.safetensors", **vram_config),
189
+ ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="video_vae_nf4.safetensors", **vram_config),
190
+ ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="audio_vae_nf4.safetensors", **vram_config),
191
+ ],
192
+ processor_config=ModelConfig(model_id="MiniMax/MiniMax-H3", origin_file_pattern="FL2VA/processor/"),
193
+ vram_limit=torch.cuda.mem_get_info("cuda")[1] / (1024 ** 3) - 4,
194
+ )
195
+ ```
196
+
197
+ prompt = "A girl is very happy, she is speaking in english: “I enjoy working with Diffsynth-Studio, it's a perfect framework.”"
198
+ video, audio = pipe(
199
+ prompt=prompt,
200
+ height=480, width=832, num_frames=124, num_inference_steps=50, seed=0,
201
+ )
202
+ write_video_audio(
203
+ video=video, audio=audio,
204
+ output_path="t2va.mp4", fps=24, audio_sample_rate=32000,
205
+ )
206
+ ```
207
+
208
+ </details>
209
+
210
+ Ref2VA:
211
+
212
+ <details>
213
+
214
+ <summary>Expand code</summary>
215
+
216
+ ```python
217
+ import torch
218
+ from PIL import Image
219
+ from diffsynth.pipelines.minimax_h3_audio_video import MiniMaxH3Pipeline, ModelConfig
220
+ from diffsynth.utils.data.audio_video import write_video_audio
221
+ from diffsynth.utils.data.audio import read_audio
222
+ from diffsynth.utils.data import VideoData
223
+ from modelscope import dataset_snapshot_download
224
+
225
+ def align_frame_count(frame_count):
226
+ current = max(int(frame_count), 1)
227
+ while current % 17 != 5:
228
+ current += 1
229
+ return current
230
+
231
+ def read_video_with_fps(path, num_out_frames, height, width, fps=24):
232
+ video = VideoData(path, height=height, width=width)
233
+ frames = video.raw_data()
234
+ src_fps = float(video.data.reader.get_meta_data()["fps"])
235
+ out = []
236
+ for k in range(num_out_frames):
237
+ idx = int(round(k * src_fps / fps))
238
+ if idx >= len(frames):
239
+ break
240
+ out.append(frames[idx])
241
+ return out
242
+
243
+ vram_config = {
244
+ "offload_dtype": "disk",
245
+ "offload_device": "disk",
246
+ "onload_dtype": torch.bfloat16,
247
+ "onload_device": "cpu",
248
+ "preparing_dtype": torch.bfloat16,
249
+ "preparing_device": "cuda",
250
+ "computation_dtype": torch.bfloat16,
251
+ "computation_device": "cuda",
252
+ }
253
+ pipe = MiniMaxH3Pipeline.from_pretrained(
254
+ torch_dtype=torch.bfloat16,
255
+ device="cuda",
256
+ model_configs=[
257
+ ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="minimax-h3-ref2va-pruned-nf4.safetensors", **vram_config),
258
+ ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="minimax-h3-text-encoder-nf4.safetensors", **vram_config),
259
+ ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="video_vae_nf4.safetensors", **vram_config),
260
+ ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="audio_vae_nf4.safetensors", **vram_config),
261
+ ],
262
+ processor_config=ModelConfig(model_id="MiniMax/MiniMax-H3", origin_file_pattern="Ref2VA/processor/"),
263
+ vram_limit=torch.cuda.mem_get_info("cuda")[1] / (1024 ** 3) - 5,
264
+ )
265
+
266
+ # Text + Reference Image -> Video + Audio
267
+ dataset_snapshot_download(dataset_id="DiffSynth-Studio/diffsynth_example_dataset", local_dir="data/diffsynth_example_dataset", allow_file_pattern="minimax_h3/MiniMax-H3-Ref2VA/*")
268
+ ref_image = Image.open("data/diffsynth_example_dataset/minimax_h3/MiniMax-H3-Ref2VA/0.png").convert("RGB")
269
+ prompt = "A website page, web UI design, website motion effects. The video demonstrates a smooth scrolling-down effect on a webpage. An intense, dynamic product landing page UI/UX demonstration video with strong visual impact, centered around Product Image 1. The layout features bold, slanted, oversized sans-serif typography. The background includes dynamic lighting effects conveying speed, along with evolving dark carbon fiber or athletic mesh textures. The video showcases a fast-paced, powerful scrolling animation, as well as strong UI interactions such as dramatic visual zooming and color inversion on mouse hover."
270
+ video, audio = pipe(
271
+ prompt=prompt,
272
+ height=480, width=832, num_frames=124, num_inference_steps=50, seed=42,
273
+ references=[{"type": "image", "image": Image.open("data/diffsynth_example_dataset/minimax_h3/MiniMax-H3-Ref2VA/0.png").convert("RGB")}]
274
+ )
275
+ write_video_audio(
276
+ video=video, audio=audio,
277
+ output_path="ti2va.mp4", fps=24, audio_sample_rate=32000,
278
+ )
279
+
280
+ # Text + Reference Audio + Reference Video -> Video + Audio
281
+ ref_video = read_video_with_fps("data/diffsynth_example_dataset/minimax_h3/MiniMax-H3-Ref2VA/video.mp4", 124, 480, 832)
282
+ ref_audio, sample_rate = read_audio("data/diffsynth_example_dataset/minimax_h3/MiniMax-H3-Ref2VA/voice.mp3", duration=len(ref_video) / 24, resample=True, resample_rate=pipe.audio_vae.sample_rate)
283
+ prompt = "subject_definitions:\n<Subject 1> is the young man with short wavy blonde hair, wearing a bright pink suit jacket, matching pink trousers, an unbuttoned white shirt, and silver rings, holding a small black lamb in his arms in <Video 1>.\n<Video 1> is the source video for the editing task.\n<Audio 1> is the synchronized audio track of <Video 1>, providing the background music.\n<Audio 2> is the voice timbre reference for <Subject 1>'s voice, containing a spoken male voiceover.\n\nsummary:\n[video editing + audio reference + audio reuse] The target video is an edited version of <Video 1>. <Subject 1>, wearing a bright pink suit and holding a black lamb, stands in a grassy field with other white lambs in the background. The edit animates <Subject 1>'s face to speak the user-provided dialogue. <Audio 1> is partially reused as the continuous background music, while the target references the calm male voice timbre of <Audio 2> for <Subject 1>'s spoken lines.\n\nretention_analysis:\n<Subject 1> (appears in [Shot 1]): fully_preserved - the man retains his identity, wavy blonde hair, pink suit, white shirt, accessories, and the black lamb he holds, with his mouth newly animated to speak.\n<Video 1> (source video editing): fully_preserved - the original camera framing, warm golden hour lighting, grassy hill setting, and background white lambs are maintained while the central character is edited.\n<Audio 1>: partially_copy - the atmospheric background music from <Audio 1> is reused in the target video, mixed beneath the newly added spoken dialogue.\n<Audio 2>: reference - the target audio references the male voice timbre from <Audio 2> to generate <Subject 1>'s spoken dialogue.\n\ndetailed_description:\nThe target video is in realistic photographic style.\n[Shot 1] The shot begins from the source <Video 1>, showing <Subject 1>, a young man with short wavy blonde hair, wearing a bright pink suit jacket, matching pink trousers, and a casually unbuttoned white shirt. He stands confidently in a sunlit green pasture, gently holding a small black lamb securely in his arms. The warm, golden hour lighting casts soft shadows across his face and the bright pink fabric of his suit. Behind him, several white lambs stand and graze on the rolling grassy hill against a clear, pale blue sky. The atmospheric background music from <Audio 1> plays continuously throughout the scene. <Subject 1> physically speaks, his mouth movements naturally syncing to the new dialogue, with his voice timbre referencing the calm male delivery from <Audio 2>. Looking thoughtfully forward, <Subject 1> (S1) speaks softly, <d>[English] Follow the wind, live free.</d> As he delivers the line, he subtly shifts his weight, cradling the resting black lamb while the camera slowly pushes in. <Subject 1> (S1) continues his thought, <d>[English] Leave worries behind, enjoy the moment.</d> Exactly as his voice stops, his lips meet in a relaxed, peaceful smile, and his jaw ceases speaking motion. He then turns his gaze slightly away toward the horizon, gently stroking the black lamb's fleece with his fingers as the camera holds on this tranquil, sunlit state through the end of the video.\n\noverall_soundscape:\nThe soundscape consists of the continuous, atmospheric background music from <Audio 1>, overlaid with the clear, calm male dialogue spoken by the main character, referencing the voice timbre of <Audio 2>.\n\nnon_diegetic_music:\nThe atmospheric, sustained background music from <Audio 1> is reused as the continuous score, playing quietly beneath the spoken dialogue."
284
+ video, audio = pipe(
285
+ prompt=prompt,
286
+ height=480, width=832, num_frames=124, num_inference_steps=50, seed=42,
287
+ references=[
288
+ {"type": "video", "video": ref_video},
289
+ {"type": "audio", "audio": ref_audio, "sample_rate": sample_rate},
290
+ ],
291
+ )
292
+ write_video_audio(
293
+ video=video, audio=audio,
294
+ output_path="tav2va.mp4", fps=24, audio_sample_rate=32000,
295
+ )
296
+
297
+ </details>
298
+
299
  ### Extreme Hardware Optimization
300
 
301
+ If your computing hardware has extremely limited performance, we support enabling direct disk-to-GPU-memory access. With this configuration, tensors in the model will be loaded sequentially from disk into GPU memory as needed for computation, allowing the model to run with only 8GB of RAM:
302
 
303
  ```diff
304
  vram_config = {
 
320
  )
321
  ```
322
 
323
+ If you wish to run model inference on Mac M-series chips, we also provide support—although this is not recommended:
324
 
325
  ```diff
326
  vram_config = {
 
344
 
345
  ## Training Code
346
 
347
+ This quantized model supports LoRA training. Follow the steps below to launch the training job.
348
 
349
+ Download the example dataset:
350
 
351
  ```shell
352
  modelscope download --dataset DiffSynth-Studio/diffsynth_example_dataset --include "minimax_h3/MiniMax-H3-FL2VA/*" --local_dir ./data/diffsynth_example_dataset
353
  ```
354
 
355
+ Training configuration suitable for data center GPUs (e.g., Nvidia H20): Run the following script to start LoRA training, which requires 48GB of GPU memory.
356
 
357
  ```shell
358
  accelerate launch examples/minimax_h3/model_training/train.py \
 
376
  --find_unused_parameters
377
  ```
378
 
379
+ Training configuration suitable for consumer-grade GPUs (e.g., Nvidia RTX 4090): Run the above script to start a two-stage split training with gradient checkpointing and offload enabled, requiring 24GB of VRAM.
380
 
381
  ```shell
382
  accelerate launch examples/minimax_h3/model_training/train.py \
 
427
  * DiffSynth-Studio Documentation: [Minimax-H3](https://diffsynth-studio-doc.readthedocs.io/en/latest/Model_Details/MiniMax-H3.html)
428
  * DiffSynth-Studio Documentation: [VRAM Management](https://diffsynth-studio-doc.readthedocs.io/en/latest/Pipeline_Usage/VRAM_management.html)
429
  * DiffSynth-Studio Documentation: [Two-Stage Split Training](https://diffsynth-studio-doc.readthedocs.io/en/latest/Training/Split_Training.html)
430
+ * DiffSynth-Studio Documentation: [Low VRAM Training](https://diffsynth-studio-doc.readthedocs.io/en/latest/Pipeline_Usage/Model_Training.html#low-vram-training)
README_from_modelscope.md CHANGED
@@ -1,51 +1,32 @@
1
- ---
2
  base_model:
 
3
  - MiniMax/MiniMax-H3
4
  frameworks:
5
  - ""
6
  license: Apache License 2.0
7
  base_model_relation: quantized
8
  ---
9
- # MiniMax-H3-NF4
10
-
11
- MiniMax-H3 多模态音视频生成模型的 **NF4 量化版本**(通过 `bitsandbytes` 4-bit 量化),配合 [DiffSynth-Studio](https://github.com/modelscope/DiffSynth-Studio) 使用,可在显存/内存受限的机器上进行「文本/图像/视频/音频 → 视频 + 音频」的联合生成。
12
-
13
- ## 文件说明
14
-
15
- | 文件 | 大小 | 作用 | 是否共用 |
16
- |---|---|---|---|
17
- | `minimax-h3-fl2va-nf4.safetensors` | ~16 GB | **FL2VA** 任务的 DiT 主干(文本 / 首尾关键帧 → 视频+音频) | FL2VA 专用 |
18
- | `minimax-h3-ref2va-nf4.safetensors` | ~16 GB | **Ref2VA** 任务的 DiT 主干(参考图像/视频/音频 → 视频+音频) | Ref2VA 专用 |
19
- | `minimax-h3-text-encoder-nf4.safetensors` | ~15 GB | Qwen3-VL 文本/视觉编码器 | 两任务共用 |
20
- | `video_vae_nf4.safetensors` | ~1.6 GB | 视频 VAE 解码器 | 两任务共用 |
21
- | `audio_vae_nf4.safetensors` | ~271 MB | 音频 VAE 解码器 | 两任务共用 |
22
 
23
- > 说明:DiT 按任务二选一,其余三个(text_encoder / video_vae / audio_vae)在两种任务下通用。加载时框架会根据文件 hash 自动识别组件类型并套用对应的量化配置(含对少数量化敏感层的 bf16 保留),无需手动指定量化参数。
24
-
25
- ## 环境要求
26
-
27
- - CUDA GPU(NF4 反量化依赖 `bitsandbytes` 的 CUDA kernel)
28
- - processor / tokenizer 需从原始仓库 `MiniMax/MiniMax-H3` 获取(下面 `processor_config`)
29
 
30
- ### 安装 DiffSynth-Studio
31
 
32
- 从源码安装(推荐,可获得最新的 MiniMax-H3 支持),并直接带上 NF4 量化依赖:
33
 
34
- ```bash
35
  git clone https://github.com/modelscope/DiffSynth-Studio.git
36
  cd DiffSynth-Studio
37
- pip install -e ".[quant]"
38
  ```
39
 
40
- ## 使用(Disk offload,低显存和内存占用)
41
 
42
- 权重存放磁盘、推理时按层流式加载到 GPU,显存占最低。**纯文本生成视频+音频(t2v)最低约 6 GB 显存即可运行。**
43
 
44
- > `vram_limit`(单位 GB)是显存阈值调小可降低显存占用(代价是更慢)
45
 
46
- > 若 CPU 内存充足,可把 `offload_device` / `offload_dtype` 改为 `"cpu"` / `torch.bfloat16`(即 CPU offload),权重常驻内存、不走磁盘,速度更快;其余代码不变。
47
-
48
- ### FL2VA — 文本 / 首尾关键帧 → 视频+音频
49
 
50
  ```python
51
  import torch
@@ -64,6 +45,7 @@ vram_config = {
64
  "computation_dtype": torch.bfloat16,
65
  "computation_device": "cuda",
66
  }
 
67
  pipe = MiniMaxH3Pipeline.from_pretrained(
68
  torch_dtype=torch.bfloat16,
69
  device="cuda",
@@ -74,10 +56,9 @@ pipe = MiniMaxH3Pipeline.from_pretrained(
74
  ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="audio_vae_nf4.safetensors", **vram_config),
75
  ],
76
  processor_config=ModelConfig(model_id="MiniMax/MiniMax-H3", origin_file_pattern="FL2VA/processor/"),
77
- vram_limit=torch.cuda.mem_get_info("cuda")[1] / (1024 ** 3) - 2,
78
  )
79
 
80
- # Text -> Video + Audio
81
  prompt = "A girl is very happy, she is speaking in english: “I enjoy working with Diffsynth-Studio, it's a perfect framework.”"
82
  video, audio = pipe(
83
  prompt=prompt,
@@ -87,34 +68,157 @@ write_video_audio(
87
  video=video, audio=audio,
88
  output_path="t2va.mp4", fps=24, audio_sample_rate=32000,
89
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
- # Text + First Frame + Last Frame -> Video + Audio
92
- dataset_snapshot_download(dataset_id="DiffSynth-Studio/diffsynth_example_dataset", local_dir="data/diffsynth_example_dataset", allow_file_pattern="minimax_h3/MiniMax-H3-FL2VA/*")
93
- first_frame = Image.open("data/diffsynth_example_dataset/minimax_h3/MiniMax-H3-FL2VA/first.png")
94
- last_frame = Image.open("data/diffsynth_example_dataset/minimax_h3/MiniMax-H3-FL2VA/last.png")
95
- prompt = "室内���庭争吵短剧场景,竖屏短剧质感,真实真人表演,中式家庭/小饭馆室内环境,暖色灯光,背景有红色装饰和书法字幅,浅景深,情绪强烈,剪辑节奏紧凑。表演要求:真实短剧表演风格,不要夸张舞台腔。男人的语气是愤怒、委屈、急切的反驳,他说“你到底想干什么?”;中老年女性的语气是尖锐、强势、咄咄逼人的质问,她说“你必须赔钱!”。两人之间有强烈对峙感,节奏逐步升级。画面风格:竖屏9:16,手机短剧质感,真人实拍感,浅景深,室内暖光,中近景为主,频繁正反打剪辑,背景保持生活化,不要科幻、不要古装、不要动画感。画面中不要出现任何字幕、文字、平台水印或贴片。 "
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  video, audio = pipe(
97
  prompt=prompt,
98
- height=832, width=480, num_frames=124, num_inference_steps=50, seed=0,
99
- keyframes=[first_frame, last_frame], keyframe_indices=[0, -1],
 
 
 
100
  )
101
  write_video_audio(
102
  video=video, audio=audio,
103
- output_path="fl2va.mp4", fps=24, audio_sample_rate=32000,
104
  )
105
  ```
106
 
107
- ### Ref2VA — 参考图像/视频/音频 → 视频+音频
 
 
108
 
109
- 支持四种参考类型,可在个列表里组合(`video` 无声带声视频用 `video_audio`):
 
 
 
 
 
 
110
 
111
  ```python
112
- {"type": "image", "image": PIL.Image}
113
- {"type": "video", "video": list[PIL.Image]} # 无声
114
- {"type": "audio", "audio": Tensor[C, L], "sample_rate": int}
115
- {"type": "video_audio", "video": list[PIL.Image], "audio": Tensor[C, L], "sample_rate": int}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  ```
117
 
 
 
 
 
 
 
 
 
118
  ```python
119
  import torch
120
  from PIL import Image
@@ -156,7 +260,7 @@ pipe = MiniMaxH3Pipeline.from_pretrained(
156
  torch_dtype=torch.bfloat16,
157
  device="cuda",
158
  model_configs=[
159
- ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="minimax-h3-ref2va-nf4.safetensors", **vram_config),
160
  ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="minimax-h3-text-encoder-nf4.safetensors", **vram_config),
161
  ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="video_vae_nf4.safetensors", **vram_config),
162
  ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="audio_vae_nf4.safetensors", **vram_config),
@@ -197,20 +301,137 @@ write_video_audio(
197
  )
198
  ```
199
 
200
- ## 常用参数
 
 
201
 
202
- - `height` / `width`:分辨率,`480x832`(横)或 `832x480`(竖)。
203
- - `num_frames`:帧数,需满足 `num_frames % 17 == 5`(如 124)。
204
- - `num_inference_steps`:去噪步数,示例用 50。
205
- - `keyframes` / `keyframe_indices`:FL2VA 首尾帧控制(`[0, -1]` 表示首帧和尾帧)。
206
- - `references`:Ref2VA 参考列表,元素为 `{"type": "image|video|audio|video_audio", ...}`。
207
- - 输出:`write_video_audio(video, audio, output_path, fps=24, audio_sample_rate=32000)`。
208
 
209
- ## 参考示例脚本
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
 
211
- 仓库内完整可运行脚本(本 README 的示例即取自这些脚本):
212
 
213
- - `examples/minimax_h3/model_inference_low_vram/MiniMax-H3-NF4-FL2VA.py`
214
- - `examples/minimax_h3/model_inference_low_vram/MiniMax-H3-NF4-Ref2VA.py`
215
- - `examples/minimax_h3/model_inference/MiniMax-H3-NF4-FL2VA.py`(CPU offload)
216
- - `examples/minimax_h3/model_inference/MiniMax-H3-NF4-Ref2VA.py`(CPU offload)
 
1
+ ---
2
  base_model:
3
+
4
  - MiniMax/MiniMax-H3
5
  frameworks:
6
  - ""
7
  license: Apache License 2.0
8
  base_model_relation: quantized
9
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
+ # MiniMax-H3-NF4
 
 
 
 
 
12
 
13
+ 本模型是视频生成模型 [MiniMax-H3](https://modelscope.cn/models/MiniMax/MiniMax-H3) 的 **NF4 量化版本**,采用 `bitsandbytes` 4-bit 量化方案,配合 [DiffSynth-Studio](https://github.com/modelscope/DiffSynth-Studio) 使用,可以在显存和内存有限的设备上运行模型推理。
14
 
15
+ ## 环境安装
16
 
17
+ ```shell
18
  git clone https://github.com/modelscope/DiffSynth-Studio.git
19
  cd DiffSynth-Studio
20
+ pip install -e ".[all]"
21
  ```
22
 
23
+ ## 推理代码
24
 
25
+ ### 用显存管理
26
 
27
+ 运行以下代码,使用 DiffSynth-Studio 进行推理,显存管理将会自动启用,实际的显存占用量取决于 GPU 上的可用显存,最低 8G 显存即可运行
28
 
29
+ FL2VA:
 
 
30
 
31
  ```python
32
  import torch
 
45
  "computation_dtype": torch.bfloat16,
46
  "computation_device": "cuda",
47
  }
48
+
49
  pipe = MiniMaxH3Pipeline.from_pretrained(
50
  torch_dtype=torch.bfloat16,
51
  device="cuda",
 
56
  ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="audio_vae_nf4.safetensors", **vram_config),
57
  ],
58
  processor_config=ModelConfig(model_id="MiniMax/MiniMax-H3", origin_file_pattern="FL2VA/processor/"),
59
+ vram_limit=torch.cuda.mem_get_info("cuda")[1] / (1024 ** 3) - 4,
60
  )
61
 
 
62
  prompt = "A girl is very happy, she is speaking in english: “I enjoy working with Diffsynth-Studio, it's a perfect framework.”"
63
  video, audio = pipe(
64
  prompt=prompt,
 
68
  video=video, audio=audio,
69
  output_path="t2va.mp4", fps=24, audio_sample_rate=32000,
70
  )
71
+ ```
72
+
73
+ Ref2VA:
74
+
75
+ <details>
76
+
77
+ <summary>展开代码</summary>
78
+
79
+ ```python
80
+ import torch
81
+ from PIL import Image
82
+ from diffsynth.pipelines.minimax_h3_audio_video import MiniMaxH3Pipeline, ModelConfig
83
+ from diffsynth.utils.data.audio_video import write_video_audio
84
+ from diffsynth.utils.data.audio import read_audio
85
+ from diffsynth.utils.data import VideoData
86
+ from modelscope import dataset_snapshot_download
87
+
88
+ def align_frame_count(frame_count):
89
+ current = max(int(frame_count), 1)
90
+ while current % 17 != 5:
91
+ current += 1
92
+ return current
93
 
94
+ def read_video_with_fps(path, num_out_frames, height, width, fps=24):
95
+ video = VideoData(path, height=height, width=width)
96
+ frames = video.raw_data()
97
+ src_fps = float(video.data.reader.get_meta_data()["fps"])
98
+ out = []
99
+ for k in range(num_out_frames):
100
+ idx = int(round(k * src_fps / fps))
101
+ if idx >= len(frames):
102
+ break
103
+ out.append(frames[idx])
104
+ return out
105
+
106
+ vram_config = {
107
+ "offload_dtype": "disk",
108
+ "offload_device": "disk",
109
+ "onload_dtype": torch.bfloat16,
110
+ "onload_device": "cpu",
111
+ "preparing_dtype": torch.bfloat16,
112
+ "preparing_device": "cuda",
113
+ "computation_dtype": torch.bfloat16,
114
+ "computation_device": "cuda",
115
+ }
116
+ pipe = MiniMaxH3Pipeline.from_pretrained(
117
+ torch_dtype=torch.bfloat16,
118
+ device="cuda",
119
+ model_configs=[
120
+ ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="minimax-h3-ref2va-nf4.safetensors", **vram_config),
121
+ ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="minimax-h3-text-encoder-nf4.safetensors", **vram_config),
122
+ ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="video_vae_nf4.safetensors", **vram_config),
123
+ ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="audio_vae_nf4.safetensors", **vram_config),
124
+ ],
125
+ processor_config=ModelConfig(model_id="MiniMax/MiniMax-H3", origin_file_pattern="Ref2VA/processor/"),
126
+ vram_limit=torch.cuda.mem_get_info("cuda")[1] / (1024 ** 3) - 5,
127
+ )
128
+
129
+ # Text + Reference Image -> Video + Audio
130
+ dataset_snapshot_download(dataset_id="DiffSynth-Studio/diffsynth_example_dataset", local_dir="data/diffsynth_example_dataset", allow_file_pattern="minimax_h3/MiniMax-H3-Ref2VA/*")
131
+ ref_image = Image.open("data/diffsynth_example_dataset/minimax_h3/MiniMax-H3-Ref2VA/0.png").convert("RGB")
132
+ prompt = "一个网站页面,网站页面UI设计,网站动效,视频展示了流畅的网页向下滚动效果。一个极具爆发力与动感的产品官网风格产品落地页 UI/UX 演示视频,核心展示主体是该产品图片1。页面采用粗犷有力、倾斜的超大号无衬线字体进行张扬的排版。背景有极具速度感的动态光影、暗色碳纤维或运动透气网眼纹理在交织变换。视频展示了节奏紧凑、充满力量感的网页向下滚动效果,以及鼠标悬停时强烈的视觉放大与颜色反转等 UI 交互动作。"
133
+ video, audio = pipe(
134
+ prompt=prompt,
135
+ height=480, width=832, num_frames=124, num_inference_steps=50, seed=42,
136
+ references=[{"type": "image", "image": Image.open("data/diffsynth_example_dataset/minimax_h3/MiniMax-H3-Ref2VA/0.png").convert("RGB")}]
137
+ )
138
+ write_video_audio(
139
+ video=video, audio=audio,
140
+ output_path="ti2va.mp4", fps=24, audio_sample_rate=32000,
141
+ )
142
+
143
+ # Text + Reference Audio + Reference Video -> Video + Audio
144
+ ref_video = read_video_with_fps("data/diffsynth_example_dataset/minimax_h3/MiniMax-H3-Ref2VA/video.mp4", 124, 480, 832)
145
+ ref_audio, sample_rate = read_audio("data/diffsynth_example_dataset/minimax_h3/MiniMax-H3-Ref2VA/voice.mp3", duration=len(ref_video) / 24, resample=True, resample_rate=pipe.audio_vae.sample_rate)
146
+ prompt = "subject_definitions:\n<Subject 1> is the young man with short wavy blonde hair, wearing a bright pink suit jacket, matching pink trousers, an unbuttoned white shirt, and silver rings, holding a small black lamb in his arms in <Video 1>.\n<Video 1> is the source video for the editing task.\n<Audio 1> is the synchronized audio track of <Video 1>, providing the background music.\n<Audio 2> is the voice timbre reference for <Subject 1>'s voice, containing a spoken male voiceover.\n\nsummary:\n[video editing + audio reference + audio reuse] The target video is an edited version of <Video 1>. <Subject 1>, wearing a bright pink suit and holding a black lamb, stands in a grassy field with other white lambs in the background. The edit animates <Subject 1>'s face to speak the user-provided dialogue. <Audio 1> is partially reused as the continuous background music, while the target references the calm male voice timbre of <Audio 2> for <Subject 1>'s spoken lines.\n\nretention_analysis:\n<Subject 1> (appears in [Shot 1]): fully_preserved - the man retains his identity, wavy blonde hair, pink suit, white shirt, accessories, and the black lamb he holds, with his mouth newly animated to speak.\n<Video 1> (source video editing): fully_preserved - the original camera framing, warm golden hour lighting, grassy hill setting, and background white lambs are maintained while the central character is edited.\n<Audio 1>: partially_copy - the atmospheric background music from <Audio 1> is reused in the target video, mixed beneath the newly added spoken dialogue.\n<Audio 2>: reference - the target audio references the male voice timbre from <Audio 2> to generate <Subject 1>'s spoken dialogue.\n\ndetailed_description:\nThe target video is in realistic photographic style.\n[Shot 1] The shot begins from the source <Video 1>, showing <Subject 1>, a young man with short wavy blonde hair, wearing a bright pink suit jacket, matching pink trousers, and a casually unbuttoned white shirt. He stands confidently in a sunlit green pasture, gently holding a small black lamb securely in his arms. The warm, golden hour lighting casts soft shadows across his face and the bright pink fabric of his suit. Behind him, several white lambs stand and graze on the rolling grassy hill against a clear, pale blue sky. The atmospheric background music from <Audio 1> plays continuously throughout the scene. <Subject 1> physically speaks, his mouth movements naturally syncing to the new dialogue, with his voice timbre referencing the calm male delivery from <Audio 2>. Looking thoughtfully forward, <Subject 1> (S1) speaks softly, <d>[English] Follow the wind, live free.</d> As he delivers the line, he subtly shifts his weight, cradling the resting black lamb while the camera slowly pushes in. <Subject 1> (S1) continues his thought, <d>[English] Leave worries behind, enjoy the moment.</d> Exactly as his voice stops, his lips meet in a relaxed, peaceful smile, and his jaw ceases speaking motion. He then turns his gaze slightly away toward the horizon, gently stroking the black lamb's fleece with his fingers as the camera holds on this tranquil, sunlit state through the end of the video.\n\noverall_soundscape:\nThe soundscape consists of the continuous, atmospheric background music from <Audio 1>, overlaid with the clear, calm male dialogue spoken by the main character, referencing the voice timbre of <Audio 2>.\n\nnon_diegetic_music:\nThe atmospheric, sustained background music from <Audio 1> is reused as the continuous score, playing quietly beneath the spoken dialogue."
147
  video, audio = pipe(
148
  prompt=prompt,
149
+ height=480, width=832, num_frames=124, num_inference_steps=50, seed=42,
150
+ references=[
151
+ {"type": "video", "video": ref_video},
152
+ {"type": "audio", "audio": ref_audio, "sample_rate": sample_rate},
153
+ ],
154
  )
155
  write_video_audio(
156
  video=video, audio=audio,
157
+ output_path="tav2va.mp4", fps=24, audio_sample_rate=32000,
158
  )
159
  ```
160
 
161
+ </details>
162
+
163
+ [Comfy-Org/MiniMax-H3](https://www.modelscope.cn/models/Comfy-Org/MiniMax-H3) 提供了 MiniMax-H3 的 pruned 变体,它把 DiT 的时间步嵌入 MLP 替换为一张查找表,使 `adaln_proj.linear` 的输入维度从 2688 降到 8,模型参数量降至约 20B。我们对其同样提供了 NF4 量化权重,单个 DiT 文件约 9.8 GB。
164
 
165
+ 用法与上文完全致,只需把 DiT 的 `origin_file_pattern` 换成 pruned 版本文本编码器与两个 VAE 仍然复用同一套权重。
166
+
167
+ FL2VA:
168
+
169
+ <details>
170
+
171
+ <summary>展开代码</summary>
172
 
173
  ```python
174
+ import torch
175
+ from diffsynth.pipelines.minimax_h3_audio_video import MiniMaxH3Pipeline, ModelConfig
176
+ from diffsynth.utils.data.audio_video import write_video_audio
177
+ from modelscope import dataset_snapshot_download
178
+ from PIL import Image
179
+
180
+ vram_config = {
181
+ "offload_dtype": "disk",
182
+ "offload_device": "disk",
183
+ "onload_dtype": torch.bfloat16,
184
+ "onload_device": "cpu",
185
+ "preparing_dtype": torch.bfloat16,
186
+ "preparing_device": "cuda",
187
+ "computation_dtype": torch.bfloat16,
188
+ "computation_device": "cuda",
189
+ }
190
+ pipe = MiniMaxH3Pipeline.from_pretrained(
191
+ torch_dtype=torch.bfloat16,
192
+ device="cuda",
193
+ model_configs=[
194
+ ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="minimax-h3-fl2va-pruned-nf4.safetensors", **vram_config),
195
+ ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="minimax-h3-text-encoder-nf4.safetensors", **vram_config),
196
+ ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="video_vae_nf4.safetensors", **vram_config),
197
+ ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="audio_vae_nf4.safetensors", **vram_config),
198
+ ],
199
+ processor_config=ModelConfig(model_id="MiniMax/MiniMax-H3", origin_file_pattern="FL2VA/processor/"),
200
+ vram_limit=torch.cuda.mem_get_info("cuda")[1] / (1024 ** 3) - 4,
201
+ )
202
+
203
+ prompt = "A girl is very happy, she is speaking in english: “I enjoy working with Diffsynth-Studio, it's a perfect framework.”"
204
+ video, audio = pipe(
205
+ prompt=prompt,
206
+ height=480, width=832, num_frames=124, num_inference_steps=50, seed=0,
207
+ )
208
+ write_video_audio(
209
+ video=video, audio=audio,
210
+ output_path="t2va.mp4", fps=24, audio_sample_rate=32000,
211
+ )
212
  ```
213
 
214
+ </details>
215
+
216
+ Ref2VA:
217
+
218
+ <details>
219
+
220
+ <summary>展开代码</summary>
221
+
222
  ```python
223
  import torch
224
  from PIL import Image
 
260
  torch_dtype=torch.bfloat16,
261
  device="cuda",
262
  model_configs=[
263
+ ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="minimax-h3-ref2va-pruned-nf4.safetensors", **vram_config),
264
  ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="minimax-h3-text-encoder-nf4.safetensors", **vram_config),
265
  ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="video_vae_nf4.safetensors", **vram_config),
266
  ModelConfig(model_id="DiffSynth-Studio/MiniMax-H3-NF4", origin_file_pattern="audio_vae_nf4.safetensors", **vram_config),
 
301
  )
302
  ```
303
 
304
+ </details>
305
+
306
+ ### 极端硬件优化
307
 
308
+ 果你的计算设备性能极为有限,我们支持开启硬盘到显存的直连,在这样的配置下,模型中的张量将会按照计算顺序逐个从硬盘加载到显存,只需 8G 内存即可运行:
 
 
 
 
 
309
 
310
+ ```diff
311
+ vram_config = {
312
+ + "offload_dtype": "disk",
313
+ + "offload_device": "disk",
314
+ + "onload_dtype": "disk",
315
+ + "onload_device": "disk",
316
+ + "preparing_dtype": "disk",
317
+ + "preparing_device": "disk",
318
+ + "computation_dtype": torch.bfloat16,
319
+ + "computation_device": "cuda",
320
+ }
321
+ pipe = MiniMaxH3Pipeline.from_pretrained(
322
+ torch_dtype=torch.bfloat16,
323
+ device="cuda",
324
+ model_configs=...,
325
+ processor_config=...,
326
+ + vram_limit=0,
327
+ )
328
+ ```
329
+
330
+ 如果你希望在 Mac M 系列芯片上运行模型推理,我们也是支持的,尽管这并不推荐:
331
+
332
+ ```diff
333
+ vram_config = {
334
+ + "offload_dtype": "disk",
335
+ + "offload_device": "disk",
336
+ + "onload_dtype": "disk",
337
+ + "onload_device": "disk",
338
+ + "preparing_dtype": "disk",
339
+ + "preparing_device": "disk",
340
+ + "computation_dtype": torch.bfloat16,
341
+ + "computation_device": "mps",
342
+ }
343
+ pipe = MiniMaxH3Pipeline.from_pretrained(
344
+ torch_dtype=torch.bfloat16,
345
+ + device="mps",
346
+ model_configs=...,
347
+ processor_config=...,
348
+ + vram_limit=0,
349
+ )
350
+ ```
351
+
352
+ ## 训练代码
353
+
354
+ 本量化模型支持 LoRA 训练,请按照以下步骤启动训练程序。
355
+
356
+ 下载样例数据集:
357
+
358
+ ```shell
359
+ modelscope download --dataset DiffSynth-Studio/diffsynth_example_dataset --include "minimax_h3/MiniMax-H3-FL2VA/*" --local_dir ./data/diffsynth_example_dataset
360
+ ```
361
+
362
+ 适合数据中心 GPU(例如 Nvidia H20)的训练配置:运行以下脚本,启动 LoRA 训练程序,需要 48G 显存。
363
+
364
+ ```shell
365
+ accelerate launch examples/minimax_h3/model_training/train.py \
366
+ --dataset_base_path data/diffsynth_example_dataset/minimax_h3/MiniMax-H3-FL2VA \
367
+ --dataset_metadata_path data/diffsynth_example_dataset/minimax_h3/MiniMax-H3-FL2VA/metadata.csv \
368
+ --data_file_keys "video,input_audio" \
369
+ --extra_inputs "input_audio" \
370
+ --height 480 \
371
+ --width 832 \
372
+ --num_frames 124 \
373
+ --dataset_repeat 100 \
374
+ --model_id_with_origin_paths "DiffSynth-Studio/MiniMax-H3-NF4:minimax-h3-text-encoder-nf4.safetensors,DiffSynth-Studio/MiniMax-H3-NF4:minimax-h3-fl2va-nf4.safetensors,DiffSynth-Studio/MiniMax-H3-NF4:video_vae_nf4.safetensors,DiffSynth-Studio/MiniMax-H3-NF4:audio_vae_nf4.safetensors" \
375
+ --learning_rate 1e-4 \
376
+ --num_epochs 5 \
377
+ --remove_prefix_in_ckpt "pipe.dit." \
378
+ --output_path "./models/train/MiniMax-H3-T2VA-nf4" \
379
+ --lora_base_model "dit" \
380
+ --lora_target_modules "qkv_proj,out_proj" \
381
+ --lora_rank 32 \
382
+ --use_gradient_checkpointing \
383
+ --find_unused_parameters
384
+ ```
385
+
386
+ 适合消费级 GPU(例如 Nvidia RTX 4090)的训练配置:运行以下脚本,启动两阶段拆分训练与 gradient checkpointing offload,需要 24G 显存。
387
+
388
+ ```shell
389
+ accelerate launch examples/minimax_h3/model_training/train.py \
390
+ --dataset_base_path data/diffsynth_example_dataset/minimax_h3/MiniMax-H3-FL2VA \
391
+ --dataset_metadata_path data/diffsynth_example_dataset/minimax_h3/MiniMax-H3-FL2VA/metadata.csv \
392
+ --data_file_keys "video,input_audio" \
393
+ --extra_inputs "input_audio" \
394
+ --height 480 \
395
+ --width 832 \
396
+ --num_frames 124 \
397
+ --dataset_repeat 1 \
398
+ --model_id_with_origin_paths "DiffSynth-Studio/MiniMax-H3-NF4:minimax-h3-text-encoder-nf4.safetensors,DiffSynth-Studio/MiniMax-H3-NF4:video_vae_nf4.safetensors,DiffSynth-Studio/MiniMax-H3-NF4:audio_vae_nf4.safetensors" \
399
+ --learning_rate 1e-4 \
400
+ --num_epochs 1 \
401
+ --remove_prefix_in_ckpt "pipe.dit." \
402
+ --output_path "./models/train/MiniMax-H3-T2VA-nf4-split-cache" \
403
+ --lora_base_model "dit" \
404
+ --lora_target_modules "qkv_proj,out_proj" \
405
+ --lora_rank 32 \
406
+ --use_gradient_checkpointing \
407
+ --use_gradient_checkpointing_offload \
408
+ --task "sft:data_process"
409
+
410
+ accelerate launch examples/minimax_h3/model_training/train.py \
411
+ --dataset_base_path "./models/train/MiniMax-H3-T2VA-nf4-split-cache" \
412
+ --data_file_keys "video,input_audio" \
413
+ --extra_inputs "input_audio" \
414
+ --height 480 \
415
+ --width 832 \
416
+ --num_frames 124 \
417
+ --dataset_repeat 100 \
418
+ --model_id_with_origin_paths "DiffSynth-Studio/MiniMax-H3-NF4:minimax-h3-fl2va-nf4.safetensors" \
419
+ --learning_rate 1e-4 \
420
+ --num_epochs 5 \
421
+ --remove_prefix_in_ckpt "pipe.dit." \
422
+ --output_path "./models/train/MiniMax-H3-T2VA-nf4" \
423
+ --lora_base_model "dit" \
424
+ --lora_target_modules "qkv_proj,out_proj" \
425
+ --lora_rank 32 \
426
+ --use_gradient_checkpointing \
427
+ --use_gradient_checkpointing_offload \
428
+ --find_unused_parameters \
429
+ --task "sft:train"
430
+ ```
431
 
432
+ ## 参考资料
433
 
434
+ * DiffSynth-Studio 文档:[Minimax-H3](https://diffsynth-studio-doc.readthedocs.io/en/latest/Model_Details/MiniMax-H3.html)
435
+ * DiffSynth-Studio 文档:[显存管理](https://diffsynth-studio-doc.readthedocs.io/en/latest/Pipeline_Usage/VRAM_management.html)
436
+ * DiffSynth-Studio 文档:[两阶段拆分训练](https://diffsynth-studio-doc.readthedocs.io/en/latest/Training/Split_Training.html)
437
+ * DiffSynth-Studio 文档:[低显存训练](https://diffsynth-studio-doc.readthedocs.io/en/latest/Pipeline_Usage/Model_Training.html#low-vram-training)
minimax-h3-fl2va-pruned-nf4.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7c7b5080f6ee907a85cb44caa233ce121e8fa84de9dbac1e24d964c2232baea4
3
+ size 10483868433
minimax-h3-ref2va-pruned-nf4.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7f32e780571a10e770676fa2c07ecc7fd0e22b35458243e456a80882c5693018
3
+ size 10483868417