> ð Click on the language section to expand / èšèªãã¯ãªãã¯ããŠå±é
# Advanced configuration / é«åºŠãªèšå®
## Table of contents / ç®æ¬¡
- [Using configuration files to specify training options](#using-configuration-files-to-specify-training-options--èšå®ãã¡ã€ã«ã䜿çšããåŠç¿ãªãã·ã§ã³ã®æå®)
- [How to specify `network_args`](#how-to-specify-network_args--network_argsã®æå®æ¹æ³)
- [LoRA+](#lora)
- [Select the target modules of LoRA](#select-the-target-modules-of-lora--loraã®å¯Ÿè±¡ã¢ãžã¥ãŒã«ãéžæãã)
- [Save and view logs in TensorBoard format](#save-and-view-logs-in-tensorboard-format--tensorboard圢åŒã®ãã°ã®ä¿åãšåç
§)
- [Save and view logs in wandb](#save-and-view-logs-in-wandb--wandbã§ãã°ã®ä¿åãšåç
§)
- [FP8 weight optimization for models](#fp8-weight-optimization-for-models--ã¢ãã«ã®éã¿ã®fp8ãžã®æé©å)
- [PyTorch Dynamo optimization for model training](#pytorch-dynamo-optimization-for-model-training--ã¢ãã«ã®åŠç¿ã«ãããpytorch-dynamoã®æé©å)
- [MagCache](#magcache)
- [Style-Friendly SNR Sampler](#style-friendly-snr-sampler)
- [Specify time step range for training](#specify-time-step-range-for-training--åŠç¿æã®ã¿ã€ã ã¹ãããç¯å²ã®æå®)
- [Timestep Bucketing for Uniform Sampling](#timestep-bucketing-for-uniform-sampling--åäžãªãµã³ããªã³ã°ã®ããã®timestep-bucketing)
- [Schedule Free Optimizer](#schedule-free-optimizer--ã¹ã±ãžã¥ãŒã«ããªãŒãªããã£ãã€ã¶)
[Post-Hoc EMA merging for LoRA](tools.md#lora-post-hoc-ema-merging--loraã®post-hoc-emaããŒãž) is described in the [Tools](tools.md) document.
## Using configuration files to specify training options / èšå®ãã¡ã€ã«ã䜿çšããåŠç¿ãªãã·ã§ã³ã®æå®
Instead of specifying all training options on the command line, you can use a `.toml` configuration file to specify them. This can make it easier to manage and reuse training configurations.
Specify the configuration file with the `--config_file` option. The `.toml` extension can be omitted.
```bash
accelerate launch --num_cpu_threads_per_process 1 --mixed_precision bf16 src/musubi_tuner/hv_train_network.py --config_file config.toml
```
The configuration file is a TOML file that can contain any of the command-line options. The file can be organized into sections for readability, but all sections are flattened when parsed, so the section names are ignored.
æ¥æ¬èª
ãã¹ãŠã®åŠç¿ãªãã·ã§ã³ãã³ãã³ãã©ã€ã³ã§æå®ãã代ããã«ã`.toml`èšå®ãã¡ã€ã«ã䜿çšããŠæå®ããããšãã§ããŸããããã«ãããåŠç¿èšå®ã®ç®¡çãåå©çšã容æã«ãªããŸãã
`--config_file`ãªãã·ã§ã³ã§èšå®ãã¡ã€ã«ãæå®ããŸãã`.toml`æ¡åŒµåã¯çç¥ã§ããŸãã
```bash
accelerate launch --num_cpu_threads_per_process 1 --mixed_precision bf16 src/musubi_tuner/hv_train_network.py --config_file config.toml
```
èšå®ãã¡ã€ã«ã¯ãã³ãã³ãã©ã€ã³ãªãã·ã§ã³ã®ãããããå«ãããšãã§ããTOMLãã¡ã€ã«ã§ãããã¡ã€ã«ã¯èªã¿ãããã®ããã«ã»ã¯ã·ã§ã³ã«åããããšãã§ããŸãããè§£ææã«ãã¹ãŠã®ã»ã¯ã·ã§ã³ããã©ããåããããããã»ã¯ã·ã§ã³åã¯ç¡èŠãããŸãã
### Example configuration file / èšå®ãã¡ã€ã«ã®äŸ
```toml
# config.toml
dit = "/path/to/dit"
dataset_config = "/path/to/dataset.toml"
network_module = "networks.lora"
network_dim = 32
network_alpha = 16
[optimizer]
optimizer_type = "AdamW"
learning_rate = 1e-4
[training]
max_train_epochs = 10
save_every_n_epochs = 2
mixed_precision = "bf16"
[output]
output_dir = "/path/to/output"
output_name = "my_lora"
logging_dir = "./logs"
```
All options can be specified in the top level or within sections. When parsed, the section structure is ignored and all key-value pairs are combined into a single namespace.
Options specified on the command line will override those in the configuration file.
```bash
# This will use the config file but override the learning_rate
accelerate launch --mixed_precision bf16 src/musubi_tuner/hv_train_network.py --config_file config --learning_rate 2e-4
```
æ¥æ¬èª
ãã¹ãŠã®ãªãã·ã§ã³ã¯ããããã¬ãã«ãŸãã¯ã»ã¯ã·ã§ã³å
ã«æå®ã§ããŸããè§£ææã«ã¯ãã»ã¯ã·ã§ã³æ§é ã¯ç¡èŠããããã¹ãŠã®ããŒãšå€ã®ãã¢ãåäžã®ããŒã ã¹ããŒã¹ã«çµåãããŸãã
ã³ãã³ãã©ã€ã³ã§æå®ããããªãã·ã§ã³ã¯ãèšå®ãã¡ã€ã«ã®ãªãã·ã§ã³ãäžæžãããŸãã
```bash
# èšå®ãã¡ã€ã«ã䜿çšããŸãããlearning_rateãäžæžãããŸã
accelerate launch --mixed_precision bf16 src/musubi_tuner/hv_train_network.py --config_file config --learning_rate 2e-4
```
## How to specify `network_args` / `network_args`ã®æå®æ¹æ³
The `--network_args` option is an option for specifying detailed arguments to LoRA. Specify the arguments in the form of `key=value` in `--network_args`.
æ¥æ¬èª
`--network_args`ãªãã·ã§ã³ã¯ãLoRAãžã®è©³çްãªåŒæ°ãæå®ããããã®ãªãã·ã§ã³ã§ãã`--network_args`ã«ã¯ã`key=value`ã®åœ¢åŒã§åŒæ°ãæå®ããŸãã
### Example / èšè¿°äŸ
If you specify it on the command line, write as follows. / ã³ãã³ãã©ã€ã³ã§æå®ããå Žåã¯ä»¥äžã®ããã«èšè¿°ããŸãã
```bash
accelerate launch --num_cpu_threads_per_process 1 --mixed_precision bf16 src/musubi_tuner/hv_train_network.py --dit ...
--network_module networks.lora --network_dim 32
--network_args "key1=value1" "key2=value2" ...
```
If you specify it in the configuration file, write as follows. / èšå®ãã¡ã€ã«ã§æå®ããå Žåã¯ä»¥äžã®ããã«èšè¿°ããŸãã
```toml
network_args = ["key1=value1", "key2=value2", ...]
```
If you specify `"verbose=True"`, detailed information of LoRA will be displayed. / `"verbose=True"`ãæå®ãããšLoRAã®è©³çŽ°ãªæ
å ±ã衚瀺ãããŸãã
```bash
--network_args "verbose=True" "key1=value1" "key2=value2" ...
```
## LoRA+
LoRA+ is a method to improve the training speed by increasing the learning rate of the UP side (LoRA-B) of LoRA. Specify the multiplier for the learning rate. The original paper recommends 16, but adjust as needed. It seems to be good to start from around 4. For details, please refer to the [related PR of sd-scripts](https://github.com/kohya-ss/sd-scripts/pull/1233).
Specify `loraplus_lr_ratio` with `--network_args`.
æ¥æ¬èª
LoRA+ã¯ãLoRAã®UPåŽïŒLoRA-BïŒã®åŠç¿çãäžããããšã§åŠç¿é床ãåäžãããææ³ã§ããåŠç¿çã«å¯Ÿããåçãæå®ããŸããå
è«æã§ã¯16ãæšå¥šããŠããŸãããå¿
èŠã«å¿ããŠèª¿æŽããŠãã ããã4çšåºŠããå§ãããšããããã§ãã詳现ã¯[sd-scriptsã®é¢é£PR]https://github.com/kohya-ss/sd-scripts/pull/1233)ãåç
§ããŠãã ããã
`--network_args`ã§`loraplus_lr_ratio`ãæå®ããŸãã
### Example / èšè¿°äŸ
```bash
accelerate launch --num_cpu_threads_per_process 1 --mixed_precision bf16 src/musubi_tuner/hv_train_network.py --dit ...
--network_module networks.lora --network_dim 32 --network_args "loraplus_lr_ratio=4" ...
```
## Select the target modules of LoRA / LoRAã®å¯Ÿè±¡ã¢ãžã¥ãŒã«ãéžæãã
*This feature is highly experimental and the specification may change. / ãã®æ©èœã¯ç¹ã«å®éšçãªãã®ã§ã仿§ã¯å€æŽãããå¯èœæ§ããããŸãã*
By specifying `exclude_patterns` and `include_patterns` with `--network_args`, you can select the target modules of LoRA.
`exclude_patterns` excludes modules that match the specified pattern. `include_patterns` targets only modules that match the specified pattern.
Specify the values as a list. For example, `"exclude_patterns=[r'.*single_blocks.*', r'.*double_blocks\.[0-9]\..*']"`.
The pattern is a regular expression for the module name. The module name is in the form of `double_blocks.0.img_mod.linear` or `single_blocks.39.modulation.linear`. The regular expression is not a partial match but a complete match.
The patterns are applied in the order of `exclude_patterns`â`include_patterns`. By default, the Linear layers of `img_mod`, `txt_mod`, and `modulation` of double blocks and single blocks are excluded.
(`.*(img_mod|txt_mod|modulation).*` is specified.)
æ¥æ¬èª
`--network_args`ã§`exclude_patterns`ãš`include_patterns`ãæå®ããããšã§ãLoRAã®å¯Ÿè±¡ã¢ãžã¥ãŒã«ãéžæããããšãã§ããŸãã
`exclude_patterns`ã¯ãæå®ãããã¿ãŒã³ã«äžèŽããã¢ãžã¥ãŒã«ãé€å€ããŸãã`include_patterns`ã¯ãæå®ãããã¿ãŒã³ã«äžèŽããã¢ãžã¥ãŒã«ã®ã¿ã察象ãšããŸãã
å€ã¯ããªã¹ãã§æå®ããŸãã`"exclude_patterns=[r'.*single_blocks.*', r'.*double_blocks\.[0-9]\..*']"`ã®ããã«ãªããŸãã
ãã¿ãŒã³ã¯ãã¢ãžã¥ãŒã«åã«å¯Ÿããæ£èŠè¡šçŸã§ããã¢ãžã¥ãŒã«åã¯ãããšãã°`double_blocks.0.img_mod.linear`ã`single_blocks.39.modulation.linear`ã®ãããªåœ¢åŒã§ããæ£èŠè¡šçŸã¯éšåäžèŽã§ã¯ãªãå®å
šäžèŽã§ãã
ãã¿ãŒã³ã¯ã`exclude_patterns`â`include_patterns`ã®é ã§é©çšãããŸããããã©ã«ãã¯ãdouble blocksãšsingle blocksã®Linearå±€ã®ãã¡ã`img_mod`ã`txt_mod`ã`modulation`ãé€å€ãããŠããŸãã
ïŒ`.*(img_mod|txt_mod|modulation).*`ãæå®ãããŠããŸããïŒ
### Example / èšè¿°äŸ
Only the modules of double blocks / double blocksã®ã¢ãžã¥ãŒã«ã®ã¿ã察象ãšããå Žå:
```bash
--network_args "exclude_patterns=[r'.*single_blocks.*']"
```
Only the modules of single blocks from the 10th / single blocksã®10çªç®ä»¥éã®Linearã¢ãžã¥ãŒã«ã®ã¿ã察象ãšããå Žå:
```bash
--network_args "exclude_patterns=[r'.*']" "include_patterns=[r'.*single_blocks\.\d{2}\.linear.*']"
```
## Save and view logs in TensorBoard format / TensorBoard圢åŒã®ãã°ã®ä¿åãšåç
§
Specify the folder to save the logs with the `--logging_dir` option. Logs in TensorBoard format will be saved.
For example, if you specify `--logging_dir=logs`, a `logs` folder will be created in the working folder, and logs will be saved in the date folder inside it.
Also, if you specify the `--log_prefix` option, the specified string will be added before the date. For example, use `--logging_dir=logs --log_prefix=lora_setting1_` for identification.
To view logs in TensorBoard, open another command prompt and activate the virtual environment. Then enter the following in the working folder.
```powershell
tensorboard --logdir=logs
```
(tensorboard installation is required.)
Then open a browser and access http://localhost:6006/ to display it.
æ¥æ¬èª
`--logging_dir`ãªãã·ã§ã³ã«ãã°ä¿åå
ãã©ã«ããæå®ããŠãã ãããTensorBoard圢åŒã®ãã°ãä¿åãããŸãã
ããšãã°`--logging_dir=logs`ãšæå®ãããšãäœæ¥ãã©ã«ãã«logsãã©ã«ããäœæããããã®äžã®æ¥æãã©ã«ãã«ãã°ãä¿åãããŸãã
ãŸã`--log_prefix`ãªãã·ã§ã³ãæå®ãããšãæ¥æã®åã«æå®ããæååã远å ãããŸãã`--logging_dir=logs --log_prefix=lora_setting1_`ãªã©ãšããŠèå¥çšã«ã䜿ããã ããã
TensorBoardã§ãã°ã確èªããã«ã¯ãå¥ã®ã³ãã³ãããã³ãããéããä»®æ³ç°å¢ãæå¹ã«ããŠãããäœæ¥ãã©ã«ãã§ä»¥äžã®ããã«å
¥åããŸãã
```powershell
tensorboard --logdir=logs
```
ïŒtensorboardã®ã€ã³ã¹ããŒã«ãå¿
èŠã§ããïŒ
ãã®åŸãã©ãŠã¶ãéããhttp://localhost:6006/ ãžã¢ã¯ã»ã¹ãããšè¡šç€ºãããŸãã
## Save and view logs in wandb / wandbã§ãã°ã®ä¿åãšåç
§
`--log_with wandb` option is available to save logs in wandb format. `tensorboard` or `all` is also available. The default is `tensorboard`.
Specify the project name with `--log_tracker_name` when using wandb.
æ¥æ¬èª
`--log_with wandb`ãªãã·ã§ã³ãæå®ãããšwandb圢åŒã§ãã°ãä¿åããããšãã§ããŸãã`tensorboard`ã`all`ãæå®å¯èœã§ããããã©ã«ãã¯`tensorboard`ã§ãã
wandbã䜿çšããå Žåã¯ã`--log_tracker_name`ã§ãããžã§ã¯ãåãæå®ããŠãã ããã
## FP8 weight optimization for models / ã¢ãã«ã®éã¿ã®FP8ãžã®æé©å
The `--fp8_scaled` option performs an offline optimization pass that rewrites selected Linear weights into FP8 (E4M3) with block-wise scaling. Compared with the legacy `--fp8` cast, it reduces VRAM usage while maintaining relatively high precision.
From v0.2.12, block-wise scaling is supported instead of per-tensor scaling, allowing for higher precision quantization.
This flow dequantizes back the weights to the FP16/BF16/FP32 weights during the forward path, and computes in FP16/BF16/FP32. The shared routines live in `src/musubi_tuner/modules/fp8_optimization_utils.py` and are wired into the Wan2.x, FramePack, FLUX.1 Kontext, and Qwen-Image pipelines (except HunyuanVideo, which `--fp8_scaled` is not supported).
Acknowledgments: This idea is based on the [implementation](https://github.com/Tencent/HunyuanVideo/blob/7df4a45c7e424a3f6cd7d653a7ff1f60cddc1eb1/hyvideo/modules/fp8_optimization.py) of [HunyuanVideo](https://github.com/Tencent/HunyuanVideo). The selection of high-precision modules is referenced from the [implementation](https://github.com/tdrussell/diffusion-pipe/blob/407c04fdae1c9ab5e67b54d33bef62c3e0a8dbc7/models/wan.py) of [diffusion-pipe](https://github.com/tdrussell/diffusion-pipe). I would like to thank these repositories.
æ¥æ¬èª
`--fp8_scaled` ãªãã·ã§ã³ã¯ã察象㮠Linear å±€ã®éã¿ããblockããšã«é©åãªåçã§ã¹ã±ãŒãªã³ã°ãã FP8 (E4M3) ã«æžãæããååŠçãå®è¡ããŸããåŸæ¥ã® `--fp8` ã«ããåçŽãªãã£ã¹ããšæ¯ã¹ãŠãå
ã®ç²ŸåºŠãæ¯èŒçä¿ã£ããŸãŸ VRAM ãåæžã§ããŸãã
v0.2.12ããããã³ãœã«ããšã®ã¹ã±ãŒãªã³ã°ã§ã¯ãªãããããã¯åäœã®ã¹ã±ãŒãªã³ã°ã«å¯Ÿå¿ããŸãããããã«ãããããé«ã粟床ã§ã®éååãå¯èœã«ãªããŸãã
forward ã®èšç®ã¯ãééååãè¡ãªã£ãéã¿ã§ FP16/BF16 ã§è¡ãããŸããå
±éã«ãŒãã³ã¯ `src/musubi_tuner/modules/fp8_optimization_utils.py` ã«ãããWan 2.xã»FramePackã»FLUX.1 Kontextã»Qwen-Image ã®åãã€ãã©ã€ã³ã§å©çšãããŸãïŒHunyuanVideo ã«ã€ããŠã¯ `--fp8_scaled` ãªãã·ã§ã³ã¯ç¡å¹ã§ãïŒã
ãã®ã¢ã€ãã¢ã¯ã[HunyuanVideo](https://github.com/Tencent/HunyuanVideo) ã® [å®è£
](https://github.com/Tencent/HunyuanVideo/blob/7df4a45c7e424a3f6cd7d653a7ff1f60cddc1eb1/hyvideo/modules/fp8_optimization.py) ã«åºã¥ããŠããŸããé«ç²ŸåºŠã¢ãžã¥ãŒã«ã®éžå®ã¯ã[diffusion-pipe](https://github.com/tdrussell/diffusion-pipe) ã® [å®è£
](https://github.com/tdrussell/diffusion-pipe/blob/407c04fdae1c9ab5e67b54d33bef62c3e0a8dbc7/models/wan.py) ãåèã«ããŠããŸãããããã®ãªããžããªã«æè¬ããŸãã
### Usage summary / äœ¿ãæ¹ã®ãŸãšã
- Inference: add `--fp8` and `--fp8_scaled` when running `wan_generate_video.py`, `fpack_generate_video.py`, `flux_kontext_generate_image.py`, or `qwen_image_generate_image.py`. HunyuanVideo continues to rely on `--fp8`/`--fp8_fast` without scaled weights.
- Training: specify `--fp8_base --fp8_scaled` in `wan_train_network.py`, `fpack_train_network.py`,`flux_kontext_train_network.py` and `qwen_image_train_network.py`; the trainers enforce this pairing.
- Input checkpoints must be FP16/BF16; pre-quantized FP8 weights cannot be re-optimized.
- LoRA / LyCORIS weights are merged before quantization, so no additional steps are required.
æ¥æ¬èª
- æšè«ã§ã¯ `wan_generate_video.py`ã`fpack_generate_video.py`ã`flux_kontext_generate_image.py`ã`qwen_image_generate_image.py` ãå®è¡ããéã« `--fp8` ãš `--fp8_scaled` ã䜵çšããŠãã ãããHunyuanVideo ã¯åŒãç¶ã`--fp8` / `--fp8_fast` ã䜿çšããã¹ã±ãŒãªã³ã°ä»ãéã¿ã¯æªå¯Ÿå¿ã§ãã
- åŠç¿ã§ã¯ `wan_train_network.py`ã`fpack_train_network.py`ã`flux_kontext_train_network.py` ã§ `--fp8_base --fp8_scaled` ãæå®ããŸãã
- èªã¿èŸŒããã§ãã¯ãã€ã³ã㯠FP16/BF16 ã§ããå¿
èŠããããŸããããããã FP8 åãããéã¿ã¯åæé©åã§ããŸããã
- LoRA / LyCORIS ã®éã¿ã¯éååã®åã«èªåã§ããŒãžãããããã远å äœæ¥ã¯äžèŠã§ãã
### Implementation highlights / å®è£
ã®ãã€ã³ã
When `--fp8_scaled` flag is enabled, the loader loads the base weights in FP16/BF16, merges optional LoRA or LyCORIS, and then emits FP8 weights plus matching block-wise `.scale_weight` buffers for the targeted layers. The patched forward either dequantizes back to the original dtype on demand for computation.
The current scripts in this repository use FP8 E4M3 format and block-wise quantization, but the implementation supports:
- Implements FP8 (E4M3 or E5M2) weight quantization for Linear layers
- Supports multiple quantization modes: tensor-wise, channel-wise, and block-wise quantization described below
- Block-wise quantization provides better precision by using granular scaling with configurable block size (default: 64)
- Reduces VRAM requirements by using 8-bit weights for storage (slightly increased compared to existing `--fp8` `--fp8_base` options)
- Quantizes weights to FP8 format with appropriate scaling instead of simple cast to FP8
- Applies monkey patching to Linear layers for transparent dequantization during computation
- Maintains computational precision by dequantizing to original precision (FP16/BF16) during forward pass
- Preserves important weights for example norm, embedding, modulation in FP16/BF16 format (fewer exclusions than previous versions)
For quantization and precision discussion, see also [Discussion #564](https://github.com/kohya-ss/musubi-tuner/discussions/564).
Note: Testing for quantization other than E4M3/block-wise is limited, so please be cautious if you plan to use the code in other projects.
æ¥æ¬èª
`--fp8_scaled` ãã©ã°ãæå¹ã«ãããšãããŒããŒã¯ãŸãããŒã¹ãšãªãéã¿ã FP16/BF16 ã®ãŸãŸèªã¿èŸŒã¿ãå¿
èŠã«å¿ã㊠LoRA ã LyCORIS ãããŒãžããåŸã察象局ã®éã¿ã FP8 ã®éã¿ãšããããã¯ããšã® `.scale_weight` ãããã¡ãžå€æããŸããforward ã§ã¯ãã®ã¹ã±ãŒã«ã䜿ã£ãŠå
ã®ç²ŸåºŠãžåçã«ééååãèšç®ãè¡ããŸãã
ãã®ãªããžããªã®çŸåšã®ã¹ã¯ãªããã§ã¯ãéååã¯FP8 E4M3圢åŒããããã¯åäœéååãçšããããŠããŸãããå®è£
ãšããŠã¯ä»¥äžããµããŒãããŠããŸãïŒ
- Linearå±€ã®FP8ïŒE4M3ãŸãã¯E5M2ïŒéã¿éååãå®è£
- è€æ°ã®éååã¢ãŒã察å¿ïŒãã³ãœã«åäœããã£ãã«åäœããããã¯åäœéåå
- ãããã¯åäœéååã¯æå®ãããããã¯ãµã€ãºïŒããã©ã«ãïŒ64ïŒã§ã®çްç²åºŠã¹ã±ãŒãªã³ã°ã«ããããé«ã粟床ãæäŸ
- 8ãããã®éã¿ã䜿çšããããšã§VRAM䜿çšéãåæžïŒæ¢åã®`--fp8` `--fp8_base` ãªãã·ã§ã³ã«æ¯ã¹ãŠåŸ®å¢ïŒ
- åçŽãªFP8ãžã®castã§ã¯ãªããé©åãªå€ã§ã¹ã±ãŒã«ããŠéã¿ãFP8圢åŒã«éåå
- Linearå±€ã«monkey patchingãé©çšããèšç®æã«ééçã«ééåå
- forwardæã«å
ã®ç²ŸåºŠïŒFP16/BF16ïŒã«ééååããŠèšç®ç²ŸåºŠãç¶æ
- 粟床ãéèŠãªéã¿ãããšãã°normãembeddingãmodulationã¯ãFP16/BF16ã®ãŸãŸä¿æïŒåŸæ¥ããŒãžã§ã³ããé€å€å¯Ÿè±¡ãåæžïŒ
éååãšç²ŸåºŠã«ã€ããŠã¯[Discussion #564](https://github.com/kohya-ss/musubi-tuner/discussions/564)ãåç
§ããŠãã ããã
â»E4M3/ãããã¯åäœä»¥å€ã®éååã®ãã¹ãã¯äžååã§ãã®ã§ãã³ãŒããä»ã®ãããžã§ã¯ãã§å©çšããå Žåçã«ã¯æ³šæããŠãã ããã
### Quantization modes / éååã¢ãŒã
The current implementation supports three quantization modes:
- **Block-wise quantization (default)**: Divides weight matrices into blocks of configurable size (default: 64) and calculates separate scale factors for each block. Provides the best precision but requires more memory for scale storage.
- **Channel-wise quantization**: Calculates scale factors per output channel (row). Balances precision and memory usage.
- **Tensor-wise quantization**: Uses a single scale factor for the entire weight tensor. Lowest memory usage but may have reduced precision for some weights.
The implementation automatically falls back to simpler modes when block-wise quantization is not feasible (e.g., when weight dimensions are not divisible by block size).
æ¥æ¬èª
çŸåšã®å®è£
ã§ã¯3ã€ã®éååã¢ãŒãããµããŒãããŠããŸãïŒ
- **ãããã¯åäœéååïŒããã©ã«ãïŒ**ïŒéã¿è¡åãèšå®å¯èœãªãµã€ãºã®ãããã¯ïŒããã©ã«ãïŒ64ïŒã«åå²ããåãããã¯ã«å¯ŸããŠåå¥ã®ã¹ã±ãŒã«ä¿æ°ãèšç®ããŸããæé«ã®ç²ŸåºŠãæäŸããŸãããã¹ã±ãŒã«ä¿åã«ãã远å ã¡ã¢ãªãå¿
èŠã§ãã
- **ãã£ãã«åäœéåå**ïŒåºåãã£ãã«ïŒè¡ïŒããšã«ã¹ã±ãŒã«ä¿æ°ãèšç®ããŸãã粟床ãšã¡ã¢ãªäœ¿çšéã®ãã©ã³ã¹ãåããŸãã
- **ãã³ãœã«åäœéåå**ïŒéã¿ãã³ãœã«å
šäœã«å¯ŸããŠåäžã®ã¹ã±ãŒã«ä¿æ°ã䜿çšããŸããæãå°ãªãã¡ã¢ãªäœ¿çšéã§ãããäžéšã®éã¿ã§ç²ŸåºŠãäœäžããå ŽåããããŸãã
å®è£
ã§ã¯ããããã¯åäœéååãå®è¡äžå¯èœãªå ŽåïŒéã¿æ¬¡å
ããããã¯ãµã€ãºã§å²ãåããªãå Žåãªã©ïŒãèªåçã«ããåçŽãªã¢ãŒãã«ãã©ãŒã«ããã¯ããŸãã
## PyTorch Dynamo optimization for model training / ã¢ãã«ã®åŠç¿ã«ãããPyTorch Dynamoã®æé©å
The PyTorch Dynamo options are now available to optimize the training process. PyTorch Dynamo is a Python-level JIT compiler designed to make unmodified PyTorch programs faster by using TorchInductor, a deep learning compiler. This integration allows for potential speedups in training while maintaining model accuracy.
[PR #215](https://github.com/kohya-ss/musubi-tuner/pull/215) added this feature.
Specify the `--dynamo_backend` option to enable Dynamo optimization with one of the available backends from the `DynamoBackend` enum.
Additional options allow for fine-tuning the Dynamo behavior:
- `--dynamo_mode`: Controls the optimization strategy
- `--dynamo_fullgraph`: Enables fullgraph mode for potentially better optimization
- `--dynamo_dynamic`: Enables dynamic shape handling
The `--dynamo_dynamic` option has been reported to have many problems based on the validation in PR #215.
### Available options:
```
--dynamo_backend {NO, INDUCTOR, NVFUSER, CUDAGRAPHS, CUDAGRAPHS_FALLBACK, etc.}
Specifies the Dynamo backend to use (default is NO, which disables Dynamo)
--dynamo_mode {default, reduce-overhead, max-autotune}
Specifies the optimization mode (default is 'default')
- 'default': Standard optimization
- 'reduce-overhead': Focuses on reducing compilation overhead
- 'max-autotune': Performs extensive autotuning for potentially better performance
--dynamo_fullgraph
Flag to enable fullgraph mode, which attempts to capture and optimize the entire model graph
--dynamo_dynamic
Flag to enable dynamic shape handling for models with variable input shapes
```
### Usage example:
```bash
python src/musubi_tuner/hv_train_network.py --dynamo_backend INDUCTOR --dynamo_mode default
```
For more aggressive optimization:
```bash
python src/musubi_tuner/hv_train_network.py --dynamo_backend INDUCTOR --dynamo_mode max-autotune --dynamo_fullgraph
```
Note: The best combination of options may depend on your specific model and hardware. Experimentation may be necessary to find the optimal configuration.
æ¥æ¬èª
PyTorch Dynamoãªãã·ã§ã³ãåŠç¿ããã»ã¹ãæé©åããããã«è¿œå ãããŸãããPyTorch Dynamoã¯ãTorchInductorïŒãã£ãŒãã©ãŒãã³ã°ã³ã³ãã€ã©ïŒã䜿çšããŠã倿Žãå ããããšãªãPyTorchããã°ã©ã ãé«éåããããã®Pythonã¬ãã«ã®JITã³ã³ãã€ã©ã§ãããã®çµ±åã«ãããã¢ãã«ã®ç²ŸåºŠãç¶æããªããåŠç¿ã®é«éåãæåŸ
ã§ããŸãã
[PR #215](https://github.com/kohya-ss/musubi-tuner/pull/215) ã§è¿œå ãããŸããã
`--dynamo_backend`ãªãã·ã§ã³ãæå®ããŠã`DynamoBackend`åæåããå©çšå¯èœãªããã¯ãšã³ãã®äžã€ãéžæããããšã§ãDynamoæé©åãæå¹ã«ããŸãã
远å ã®ãªãã·ã§ã³ã«ãããDynamoã®åäœã埮調æŽã§ããŸãïŒ
- `--dynamo_mode`ïŒæé©åæŠç¥ãå¶åŸ¡ããŸã
- `--dynamo_fullgraph`ïŒããè¯ãæé©åã®å¯èœæ§ã®ããã«ãã«ã°ã©ãã¢ãŒããæå¹ã«ããŸã
- `--dynamo_dynamic`ïŒåç圢ç¶åŠçãæå¹ã«ããŸã
PR #215ã§ã®æ€èšŒã«ãããšã`--dynamo_dynamic`ã«ã¯åé¡ãå€ãããšãå ±åãããŠããŸãã
__å©çšå¯èœãªãªãã·ã§ã³ïŒ__
```
--dynamo_backend {NO, INDUCTOR, NVFUSER, CUDAGRAPHS, CUDAGRAPHS_FALLBACK, ãªã©}
䜿çšããDynamoããã¯ãšã³ããæå®ããŸãïŒããã©ã«ãã¯NOã§ãDynamoãç¡å¹ã«ããŸãïŒ
--dynamo_mode {default, reduce-overhead, max-autotune}
æé©åã¢ãŒããæå®ããŸãïŒããã©ã«ã㯠'default'ïŒ
- 'default'ïŒæšæºçãªæé©å
- 'reduce-overhead'ïŒã³ã³ãã€ã«ã®ãªãŒããŒãããåæžã«çŠç¹ãåœãŠã
- 'max-autotune'ïŒããè¯ãããã©ãŒãã³ã¹ã®ããã«åºç¯ãªèªå調æŽãå®è¡
--dynamo_fullgraph
ãã«ã°ã©ãã¢ãŒããæå¹ã«ãããã©ã°ãã¢ãã«ã°ã©ãå
šäœããã£ããã£ããŠæé©åããããšããŸã
--dynamo_dynamic
å¯å€å
¥å圢ç¶ãæã€ã¢ãã«ã®ããã®åç圢ç¶åŠçãæå¹ã«ãããã©ã°
```
__䜿çšäŸïŒ__
```bash
python src/musubi_tuner/hv_train_network.py --dynamo_backend INDUCTOR --dynamo_mode default
```
ããç©æ¥µçãªæé©åã®å ŽåïŒ
```bash
python src/musubi_tuner/hv_train_network.py --dynamo_backend INDUCTOR --dynamo_mode max-autotune --dynamo_fullgraph
```
泚æïŒæé©ãªãªãã·ã§ã³ã®çµã¿åããã¯ãç¹å®ã®ã¢ãã«ãšããŒããŠã§ã¢ã«äŸåããå ŽåããããŸããæé©ãªæ§æãèŠã€ããããã«å®éšãå¿
èŠãããããŸããã
## MagCache
The following is quoted from the [MagCache github repository](https://github.com/Zehong-Ma/MagCache) "Magnitude-aware Cache (MagCache) for Video Diffusion Models":
> We introduce Magnitude-aware Cache (MagCache), a training-free caching approach that estimates and leverages the fluctuating differences among model outputs across timesteps based on the robust magnitude observations, thereby accelerating the inference. MagCache works well for Video Diffusion Models, Image Diffusion models.
We have implemented the MagCache feature in Musubi Tuner. Some of the code is based on the MagCache repository. It is available for `fpack_generate_video.py` for now.
### Usage
1. Calibrate the mag ratios
- Run the inference script as normal, but with the `--magcache_calibration` option to calibrate the mag ratios. You will get a following output:
```
INFO:musubi_tuner.fpack_generate_video:Copy and paste following values to --magcache_mag_ratios argument to use them:
1.00000,1.26562,1.08594,1.02344,1.00781,1.01562,1.01562,1.03125,1.04688,1.00781,1.03125,1.00000,1.01562,1.01562,1.02344,1.01562,0.98438,1.05469,0.98438,0.97266,1.03125,0.96875,0.93359,0.95703,0.77734
```
- It is recommended to run the calibration with your custom prompt and model.
- If you inference the multi-section video, you will get the mag ratios for each section. You can use the one of the sections or average them.
2. Use the mag ratios
- Run the inference script with the `--magcache_mag_ratios` option to use the mag ratios. For example:
```bash
python fpack_generate_video.py --magcache_mag_ratios 1.00000,1.26562,1.08594,1.02344,1.00781,1.01562,1.01562,1.03125,1.04688,1.00781,1.03125,1.00000,1.01562,1.01562,1.02344,1.01562,0.98438,1.05469,0.98438,0.97266,1.03125,0.96875,0.93359,0.95703,0.77734
```
- Specify `--magcache_mag_ratios 0` to use the default mag ratios from the MagCache repository.
- It is recommended to use the same steps as the calibration. If the steps are different, the mag ratios is interpolated to the specified steps.
- You can also specify the `--magcache_retention_ratio`, `--magcache_threshold`, and `--magcache_k` options to control the MagCache behavior. The default values are 0.2, 0.24, and 6, respectively (same as the MagCache repository).
```bash
python fpack_generate_video.py --magcache_retention_ratio 0.2 --magcache_threshold 0.24 --magcache_k 6
```
- The `--magcache_retention_ratio` option controls the ratio of the steps not to cache. For example, if you set it to 0.2, the first 20% of the steps will not be cached. The default value is 0.2.
- The `--magcache_threshold` option controls the threshold whether to use the cached output or not. If the accumulated error is less than the threshold, the cached output will be used. The default value is 0.24.
- The error is calculated by the accumulated error multiplied by the mag ratio.
- The `--magcache_k` option controls the number of steps to use for the cache. The default value is 6, which means the consecutive 6 steps will be used for the cache. The default value 6 is recommended for 50 steps, so you may want to lower it for smaller number of steps.
### Generated video example
Using F1-model, without MagCache, approximately 90 seconds are required to generate single section video with 25 steps (without VAE decoding) in my environment.
https://github.com/user-attachments/assets/30b8d05e-9bd6-42bf-997f-5ba5b3dde876
With MagCache, default settings, approximately 30 seconds are required to generate with the same settings.
https://github.com/user-attachments/assets/080076ea-4088-443c-8138-4eeb00694ec5
With MagCache, `--magcache_retention_ratio 0.2 --magcache_threshold 0.12 --magcache_k 3`, approximately 35 seconds are required to generate with the same settings.
https://github.com/user-attachments/assets/27d6c7ff-e3db-4c52-8668-9a887441acef
æ¥æ¬èª
以äžã¯ã[MagCache githubãªããžããª](https://github.com/Zehong-Ma/MagCache) "Magnitude-aware Cache (MagCache) for Video Diffusion Models"ããã®åŒçšã®æèš³ã§ãïŒ
> Magnitude-aware Cache (MagCache)ã¯ããã¬ãŒãã³ã°äžèŠã®ãã£ãã·ã³ã°ã¢ãããŒãã§ãå
ç¢ãªãã°ããã¥ãŒã芳枬ã«åºã¥ããŠã¿ã€ã ã¹ãããéã®ã¢ãã«åºåã®å€åå·®ãæšå®ããã³æŽ»çšããæšè«ãå éããŸããMagCacheã¯ããããªæ¡æ£ã¢ãã«ãç»åæ¡æ£ã¢ãã«ã«é©ããŠããŸãã
Musubi Tunerã«MagCacheæ©èœãå®è£
ããŸãããäžéšã®ã³ãŒãã¯MagCacheãªããžããªã®ã³ãŒããåºã«ããŠããŸããçŸåšã¯`fpack_generate_video.py`ã§ã®ã¿å©çšå¯èœã§ãã
### äœ¿çšæ¹æ³
1. mag_ratiosã®ãã£ãªãã¬ãŒã·ã§ã³
- `--magcache_calibration`ãªãã·ã§ã³ãæå®ããŠããã以å€ã¯éåžžéãæšè«ã¹ã¯ãªãããå®è¡ããmag ratiosããã£ãªãã¬ãŒã·ã§ã³ããŸãã以äžã®ãããªåºåãåŸãããŸãïŒ
```
INFO:musubi_tuner.fpack_generate_video:Copy and paste following values to --magcache_mag_ratios argument to use them:
1.00000,1.26562,1.08594,1.02344,1.00781,1.01562,1.01562,1.03125,1.04688,1.00781,1.03125,1.00000,1.01562,1.01562,1.02344,1.01562,0.98438,1.05469,0.98438,0.97266,1.03125,0.96875,0.93359,0.95703,0.77734
```
- ã«ã¹ã¿ã ããã³ãããšã¢ãã«ã§ãã£ãªãã¬ãŒã·ã§ã³ãå®è¡ããããšããå§ãããŸãã
- è€æ°ã»ã¯ã·ã§ã³ãããªãæšè«ããå Žåãåã»ã¯ã·ã§ã³ã®mag ratiosãåºåãããŸããã©ããäžã€ããŸãã¯ããããå¹³åããå€ã䜿ã£ãŠãã ããã
2. mag ratiosã®äœ¿çš
- `--magcache_mag_ratios`ãªãã·ã§ã³ã§mag ratiosãæå®ããŠæšè«ã¹ã¯ãªãããå®è¡ããŸããäŸïŒ
```bash
python fpack_generate_video.py --magcache_mag_ratios 1.00000,1.26562,1.08594,1.02344,1.00781,1.01562,1.01562,1.03125,1.04688,1.00781,1.03125,1.00000,1.01562,1.01562,1.02344,1.01562,0.98438,1.05469,0.98438,0.97266,1.03125,0.96875,0.93359,0.95703,0.77734
```
- `--magcache_mag_ratios 0`ãæå®ãããšãMagCacheãªããžããªã®ããã©ã«ãã®mag ratiosã䜿çšãããŸãã
- mag ratiosã®æ°ã¯ãã£ãªãã¬ãŒã·ã§ã³ããæãšåãã¹ãããæ°ãæå®ããããšããå§ãããŸããã¹ãããæ°ãç°ãªãå Žåãmag ratiosã¯æå®ãããã¹ãããæ°ã«åãããã«è£éãããŸãã
- `--magcache_retention_ratio`, `--magcache_threshold`, `--magcache_k`ãªãã·ã§ã³ãæå®ããŠMagCacheã®åäœãå¶åŸ¡ã§ããŸããããã©ã«ãå€ã¯0.2ã0.24ã6ã§ãïŒMagCacheãªããžããªãšåãã§ãïŒã
```bash
python fpack_generate_video.py --magcache_retention_ratio 0.2 --magcache_threshold 0.24 --magcache_k 6
```
- `--magcache_retention_ratio`ãªãã·ã§ã³ã¯ããã£ãã·ã¥ããªãã¹ãããã®å²åãå¶åŸ¡ããŸããäŸãã°ã0.2ã«èšå®ãããšãæåã®20%ã®ã¹ãããã¯ãã£ãã·ã¥ãããŸãããããã©ã«ãå€ã¯0.2ã§ãã
- `--magcache_threshold`ãªãã·ã§ã³ã¯ããã£ãã·ã¥ãããåºåã䜿çšãããã©ããã®éŸå€ãå¶åŸ¡ããŸãã环ç©èª€å·®ããã®éŸå€æªæºã®å Žåããã£ãã·ã¥ãããåºåã䜿çšãããŸããããã©ã«ãå€ã¯0.24ã§ãã
- 誀差ã¯ã环ç©èª€å·®ã«mag ratioãæãããã®ãšããŠèšç®ãããŸãã
- `--magcache_k`ãªãã·ã§ã³ã¯ããã£ãã·ã¥ã«äœ¿çšããã¹ãããæ°ãå¶åŸ¡ããŸããããã©ã«ãå€ã¯6ã§ãããã¯é£ç¶ãã6ã¹ãããããã£ãã·ã¥ã«äœ¿çšãããããšãæå³ããŸããããã©ã«ãå€6ã¯æãã50ã¹ãããã®å Žåã®æšå¥šå€ã®ãããã¹ãããæ°ãå°ãªãå Žåã¯æžããããšãæ€èšããŠãã ããã
çæãµã³ãã«ã¯è±èªã§ã®èª¬æãåç
§ããŠãã ããã
## Style-Friendly SNR Sampler
This sampler is based on the paper [Style-Friendly SNR Sampler for Style-Driven Generation](https://arxiv.org/abs/2411.14793). The paper argues that stylistic features in diffusion models are predominantly learned at high noise levels. This sampler biases the noise level (timestep) sampling towards these higher noise levels, which can significantly improve the model's ability to learn and reproduce specific styles.
This feature is enabled by specifying `--timestep_sampling`.
æ¥æ¬èª
ãã®ãµã³ãã©ãŒã¯ãè«æã[Style-Friendly SNR Sampler for Style-Driven Generation](https://arxiv.org/abs/2411.14793)ãã«åºã¥ããŠããŸãããã®è«æã§ã¯ãæ¡æ£ã¢ãã«ã«ãããã¹ã¿ã€ã«ç¹åŸŽã¯ãäž»ã«ãã€ãºã¬ãã«ãé«ãé åã§åŠç¿ããããšäž»åŒµããŠããŸãããã®ãµã³ãã©ãŒã¯ããã€ãºã¬ãã«ïŒã¿ã€ã ã¹ãããïŒã®ãµã³ããªã³ã°ãæå³çã«é«ãã€ãºã¬ãã«åŽã«åãããããšã§ãã¢ãã«ãç¹å®ã®ã¹ã¿ã€ã«ãåŠç¿ã»åçŸããèœåã倧å¹
ã«åäžãããããšãã§ããŸãã
ãã®æ©èœã¯ `--timestep_sampling` ãæå®ããããšã§æå¹ã«ãªããŸãã
### `logsnr` Sampler
This is a direct implementation of the sampler proposed in the paper. It samples the log-SNR value from a normal distribution. By setting a low mean and a large standard deviation, it focuses the training on high-noise levels crucial for style learning.
To use this, specify `logsnr` for `--timestep_sampling`. You can also configure the mean and standard deviation of the log-SNR distribution with `--logit_mean` and `--logit_std`.
The paper recommends `logit_mean=-6.0` and `logit_std` of 2.0 or 3.0.
```bash
accelerate launch ... \
--timestep_sampling logsnr \
--logit_mean -6.0 \
--logit_std 2.0
```
Following is the distribution of the logsnr sampler:

æ¥æ¬èª
è«æã§ææ¡ãããéãã®ãµã³ãã©ãŒã®å®è£
ã§ããlog-SNRå€ãæ£èŠååžãããµã³ããªã³ã°ããŸããäœãå¹³åå€ãšå€§ããªæšæºåå·®ãèšå®ããããšã§ãã¹ã¿ã€ã«ã®åŠç¿ã«äžå¯æ¬ ãªé«ãã€ãºã¬ãã«é åã«åŠç¿ãéäžãããŸãã
䜿çšããã«ã¯ã`--timestep_sampling` ã« `logsnr` ãæå®ããŸãããŸãã`--logit_mean` ãš `--logit_std` ã§log-SNRååžã®å¹³åãšæšæºåå·®ãèšå®ã§ããŸãã
è«æã§ã¯ `logit_mean=-6.0`ã`logit_std` ã¯2.0ãŸãã¯3.0ãæšå¥šãããŠããŸãã
### `qinglong_flux` and `qinglong_qwen` Sampler (Hybrid Sampler)
This is a hybrid sampling method that combines three different samplers to balance style learning, model stability, and detail preservation. It is an experimental feature inspired by the Style-Friendly SNR Sampler. It was proposed by sdbds (Qing Long) in PR [#407](https://github.com/kohya-ss/musubi-tuner/pull/407).
In each training step, one of the following samplers is chosen for each sample in the batch based on a predefined ratio:
1. **flux_shift or qwen_shift (80%)**: The standard sampler for high-resolution models. Focuses on overall stability.
2. **logsnr (7.5%)**: The Style-Friendly sampler. Focuses on style learning.
3. **logsnr2 (12.5%)**: A sampler that focuses on low-noise regions (high log-SNR values). Aims to improve the learning of fine details.
To use this, specify `qinglong_flux` or `qinglong_qwen` for `--timestep_sampling`.
```bash
accelerate launch ... \
--timestep_sampling qinglong_flux \
--logit_mean -6.0 \
--logit_std 2.0
```
Following is the distribution of the qinglong flux sampler:

æ¥æ¬èª
ããã¯ãã¹ã¿ã€ã«ã®åŠç¿ãã¢ãã«ã®å®å®æ§ããã£ããŒã«ã®åçŸæ§ã®ãã©ã³ã¹ãåãããã«ã3ã€ã®ç°ãªããµã³ãã©ãŒãçµã¿åããããã€ããªãããµã³ããªã³ã°ææ³ã§ããStyle-Friendly SNR Samplerã«ã€ã³ã¹ãã€ã¢ãããå®éšçãªæ©èœã§ããPR [#407](https://github.com/kohya-ss/musubi-tuner/pull/407) ã§ sdbds (Qing Long) æ°ã«ããææ¡ãããŸããã
ååŠç¿ã¹ãããã«ãããŠããããå
ã®åãµã³ãã«ã«å¯ŸããŠããããããå®çŸ©ãããæ¯çã«åºã¥ã以äžã®ããããã®ãµã³ãã©ãŒãéžæãããŸãã
1. **flux_shift ãŸã㯠qwen_shift (80%)**: é«è§£å床ã¢ãã«åãã®æšæºçãªãµã³ãã©ãŒãå
šäœçãªå®å®æ§ãéèŠããŸãã
2. **logsnr (7.5%)**: Style-Friendlyãµã³ãã©ãŒãã¹ã¿ã€ã«ã®åŠç¿ãéèŠããŸãã
3. **logsnr2 (12.5%)**: äœãã€ãºé åïŒé«ãlog-SNRå€ïŒã«çŠç¹ãåœãŠããµã³ãã©ãŒã现éšã®ãã£ããŒã«åŠç¿ãåäžãããããšãç®çãšããŸãã
䜿çšããã«ã¯ã`--timestep_sampling` ã« `qinglong_flux` ãŸã㯠`qinglong_qwen` ãæå®ããŸãã
## Specify time step range for training / åŠç¿æã®ã¿ã€ã ã¹ãããç¯å²ã®æå®
You can specify the range of timesteps for training. This is useful for focusing the training on a specific part of the diffusion process.
- `--min_timestep`: Specifies the minimum timestep for training (0-999, default: 0).
- `--max_timestep`: Specifies the maximum timestep for training (1-1000, default: 1000).
- `--preserve_distribution_shape`: If specified, it constrains timestep sampling to the `[min_timestep, max_timestep]` range using rejection sampling, which preserves the original distribution shape. By default, the `[0, 1]` range is scaled, which can distort the distribution. This option is only effective when `timestep_sampling` is not 'sigma'.
æ¥æ¬èª
åŠç¿æã®ã¿ã€ã ã¹ãããã®ç¯å²ãæå®ã§ããŸããããã«ãããæ¡æ£ããã»ã¹ã®ç¹å®ã®éšåã«åŠç¿ãéäžãããããšãã§ããŸãã
- `--min_timestep`: åŠç¿æã®æå°ã¿ã€ã ã¹ããããæå®ããŸãïŒ0-999ãããã©ã«ã: 0ïŒã
- `--max_timestep`: åŠç¿æã®æå€§ã¿ã€ã ã¹ããããæå®ããŸãïŒ1-1000ãããã©ã«ã: 1000ïŒã
- `--preserve_distribution_shape`: æå®ãããšãã¿ã€ã ã¹ãããã®ãµã³ããªã³ã°ãæ£åŽãµã³ããªã³ã°ïŒæ¡ä»¶ã«åããªããã®ãæšãŠãïŒãçšã㊠`[min_timestep, max_timestep]` ã®ç¯å²ã«å¶çŽããå
ã®ååžåœ¢ç¶ãä¿æããŸããããã©ã«ãã§ã¯ã`[0, 1]` ã®ç¯å²ãã¹ã±ãŒãªã³ã°ããããããååžãæªãå¯èœæ§ããããŸãããã®ãªãã·ã§ã³ã¯ `timestep_sampling` ã 'sigma' 以å€ã®å Žåã«ã®ã¿æå¹ã§ãã
### Example / èšè¿°äŸ
To train only on the latter half of the timesteps (more detailed part) / ã¿ã€ã ã¹ãããã®åŸåïŒãã詳现ãªéšåïŒã®ã¿ãåŠç¿ããå Žå:
```bash
--min_timestep 500 --max_timestep 1000
```
To train only on the first half of the timesteps (more structural part) / ã¿ã€ã ã¹ãããã®ååïŒããæ§é çãªéšåïŒã®ã¿ãåŠç¿ããå Žå:
```bash
--min_timestep 0 --max_timestep 500
```
To train on a specific range while preserving the sampling distribution shape / ãµã³ããªã³ã°ååžã®åœ¢ç¶ãç¶æãã€ã€ç¹å®ã®ç¯å²ã§åŠç¿ããå Žå:
```bash
--min_timestep 200 --max_timestep 800 --preserve_distribution_shape
```
### Actual distribution shape / å®éã®ååžåœ¢ç¶
You can visualize the distribution shape of the timesteps with `--show_timesteps image` (or console) option. The distribution shape is determined by the `--min_timestep`, `--max_timestep`, and `--preserve_distribution_shape` options.
In the following examples, the discrete flow shift is set to 3.0.
When `--min_timestep` and `--max_timestep` are not specified, the distribution shape is as follows:

When `--min_timestep 500` and `--max_timestep 100` are specified, and `--preserve_distribution_shape` is not specified, the distribution shape is as follows:

When `--min_timestep 500` and `--max_timestep 100` are specified, and `--preserve_distribution_shape` is specified, the distribution shape is as follows:

æ¥æ¬èª
ã¿ã€ã ã¹ãããã®ååžåœ¢ç¶ã¯ã`--show_timesteps image`ïŒãŸãã¯consoleïŒãªãã·ã§ã³ã§ç¢ºèªã§ããŸããååžåœ¢ç¶ã¯ã`--min_timestep`ã`--max_timestep`ãããã³ `--preserve_distribution_shape` ãªãã·ã§ã³ã«ãã£ãŠæ±ºãŸããŸãã
äžã®å³ã¯ããããã颿£ãããŒã·ããã3.0ã®ãšãã
1. `--min_timestep` ãš `--max_timestep` ãæå®ãããŠããªãå Žå
2. `--min_timestep 500` ãš `--max_timestep 1000` ãæå®ããã`--preserve_distribution_shape` ãæå®ãããŠããªãå Žå
3. `--min_timestep 500` ãš `--max_timestep 1000` ãæå®ããã`--preserve_distribution_shape` ãæå®ãããå Žå
ã®ååžåœ¢ç¶ã瀺ããŠããŸãã
## Timestep Bucketing for Uniform Sampling / åäžãªãµã³ããªã³ã°ã®ããã®Timestep Bucketing
This feature is experimental.
When training with a small dataset or for a few epochs, the random sampling of timesteps can be biased, potentially leading to unstable training. To mitigate this, timestep bucketing ensures a more uniform distribution of timesteps throughout the training process.
This feature works as follows:
1. At the beginning of each epoch, it prepares a pool of timesteps equal to the number of items in the dataset for that epoch. These timesteps are calculated as follows:
- A specified number of buckets is created. Each bucket represents an equal interval of the `[0, 1]` range (e.g., with 5 buckets, the ranges are `[0, 0.2]`, `[0.2, 0.4]`, ... `[0.8, 1.0]`).
- Each bucket is filled with an equal number of randomly generated timesteps within its range.
- The number of timesteps in each bucket is calculated as "number of dataset items ÷ number of buckets".
2. All timesteps from all buckets are then combined and shuffled.
3. During training, instead of generating a random timestep for each item, one is drawn from this pre-shuffled pool.
This ensures that the model sees a balanced distribution of timesteps in each epoch, which can improve training stability, especially for LoRA training or when using small datasets.
This feature is enabled by specifying `--num_timestep_buckets`.
æ¥æ¬èª
ãã®æ©èœã¯å®éšçãªãã®ã§ãã
ããŒã¿ã»ãããå°ããå ŽåãåŠç¿ãšããã¯æ°ãå°ãªãå Žåãã¿ã€ã ã¹ãããã®ä¹±æ°ã«åããçããããšã§ãåŠç¿ãäžå®å®ã«ãªãå¯èœæ§ããããŸããTimestep Bucketingæ©èœã¯ããã®åé¡ã軜æžããããã®æ©èœã§ãåŠç¿ããã»ã¹å
šäœã§ã¿ã€ã ã¹ããããããåäžã«ååžãããã調æŽããŸãã
ãã®æ©èœã¯ä»¥äžã®ããã«åäœããŸãïŒ
1. åãšããã¯ã®éå§æã«ãããããããã®ãšããã¯ã®ããŒã¿ã»ããã®ä»¶æ°ãšåãæ°ã®ãã¿ã€ã ã¹ããããæºåããŸãããããã®ã¿ã€ã ã¹ãããã¯ä»¥äžã®ããã«èšç®ãããŸãã
- æå®ãããæ°ã®ãã±ãããæºåããŸããåãã±ãã㯠`[0, 1]` ã®ç¯å²ãçåããåºéã衚ããŸãïŒäŸïŒ5ãã±ããã®å Žåã`[0, 0.2]`ã`[0.2, 0.4]` ... `[0.8, 1.0]`ïŒã
- åãã±ããã«ããã®ç¯å²å
ã§ã©ã³ãã ã«çæãããã¿ã€ã ã¹ããããé
眮ããŸãã
- ããããã®ãã±ããã®ã¿ã€ã ã¹ãããã®ä»¶æ°ã¯ããããŒã¿ã»ããã®ä»¶æ°Ã·ãã±ããæ°ãã§èšç®ãããŸãã
2. ãã¹ãŠã®ãã±ããã®ã¿ã€ã ã¹ããããçµåãããã·ã£ããã«ãããŸãã
3. åŠç¿æã«ã¯ãã¢ã€ãã ããšã«ã©ã³ãã ãªã¿ã€ã ã¹ããããçæãã代ããã«ããã®äºåã«ã·ã£ããã«ãããããŒã«ããã¿ã€ã ã¹ããããåãåºãããŸãã
ããã«ãããåãšããã¯ã§ã¢ãã«ããã©ã³ã¹ã®åããã¿ã€ã ã¹ãããã®ååžã䜿çšããããšã«ãªããç¹ã«LoRAã®åŠç¿ãå°èŠæš¡ãªããŒã¿ã»ããã䜿çšããéã®åŠç¿ã®å®å®æ§ãåäžããŸãã
ãã®æ©èœã¯ `--num_timestep_buckets` ãæå®ããããšã§æå¹ã«ãªããŸãã
### How to use / äœ¿çšæ¹æ³
Specify the number of buckets with the `--num_timestep_buckets` option. A value of 2 or more enables this feature. If not specified, it is disabled.
The community research is required to determine the optimal value, but starting with a value between `4` and `10` may be a good idea.
æ¥æ¬èª
`--num_timestep_buckets` ãªãã·ã§ã³ã§ãã±ããæ°ãæå®ããŸãã2以äžã®å€ãæå®ãããšãã®æ©èœãæå¹ã«ãªããŸããæå®ããªãå Žåã¯ç¡å¹ã§ãã
æé©ãªå€ã«é¢ããŠã¯ã³ãã¥ããã£ã®æ€èšŒãå¿
èŠã§ããã`4` ãã `10` çšåºŠã®å€ããå§ãããšè¯ããšæãããŸãã
### Example / èšè¿°äŸ
```bash
accelerate launch ... \
--num_timestep_buckets 5
```
### Notes / 泚æç¹
- This feature may not work as expected when training with both high and low noise models simultaneously in `wan_train_network.py` (`--dit_high_noise` option) or when `--preserve_distribution_shape` is specified. Because the way timesteps are handled will differ in these cases.
Specifically, instead of selecting from pre-configured timestep buckets, the process involves determining buckets on-demand and generating random timesteps within the range each bucket covers. Therefore, the uniform sampling effect may not be achieved, but some improvement can be expected compared to completely random generation (within the `[0, 1]` range).
æ¥æ¬èª
- `wan_train_network.py` ã§high/lowãã€ãºã¢ãã«ãåæã«åŠç¿ããå ŽåïŒ`--dit_high_noise` ãªãã·ã§ã³ïŒãããã³ã`--preserve_distribution_shape` ãæå®ããå Žåãã¿ã€ã ã¹ãããã®æ±ããç°ãªãããããã®æ©èœã¯æåŸ
éãã«åäœããªãå¯èœæ§ããããŸãã
å
·äœçã«ã¯ããããããèšå®ãããã¿ã€ã ã¹ãããã®ãã±ããããéžæãããã®ã§ã¯ãªããéœåºŠããã±ãã®æ±ºå®âç¯å²å
ã§ã®ã©ã³ãã ãªã¿ã€ã ã¹ãããã®çæãè¡ãããŸãããã®ãããåäžãªãµã³ããªã³ã°ã®å¹æãåŸãããªãå¯èœæ§ããããŸãããå®å
šãªã©ã³ãã ïŒ`[0, 1]` ã®ç¯å²ã§ã®çæïŒã«æ¯ã¹ããšãå€å°ã®æ¹åãèŠèŸŒãŸããŸãã
## Schedule Free Optimizer / ã¹ã±ãžã¥ãŒã«ããªãŒãªããã£ãã€ã¶
[Schedule Free Optimizer](https://github.com/facebookresearch/schedule_free) is an optimizer that does not require a learning rate schedule.
The library is optional, so you can install it with `pip install schedulefree`.
Specify the optimizer with the `--optimizer_type` argument, using the format `package_name.ClassName`, for example: `--optimizer_type schedulefree.AdamWScheduleFree`.
You can specify multiple arguments for the optimizer using the `--optimizer_args` argument in the form `arg_name=value` (e.g., `--optimizer_args "weight_decay=0.01" "betas=(0.9,0.95)"`).
æ¥æ¬èª
[Schedule Free Optimizer](https://github.com/facebookresearch/schedule_free)ã¯ãåŠç¿çã¹ã±ãžã¥ãŒã«ãå¿
èŠãšããªããªããã£ãã€ã¶ã§ãã
ã©ã€ãã©ãªã¯ãªãã·ã§ã³ã®ããã`pip install schedulefree` ã§ã€ã³ã¹ããŒã«ããŠãã ããã
`--optimizer_type`åŒæ°ã«ã` --optimizer_type schedulefree.AdamWScheduleFree`ã®ããã«ã`ããã±ãŒãžå.ã¯ã©ã¹å`ã®åœ¢åŒã§æå®ããŸãããªããã£ãã€ã¶ãžã®åŒæ°ã¯ã`--optimizer_args`ã«`åŒæ°å=å€`ã®åœ¢ã§è€æ°æå®ã§ããŸãïŒäŸïŒ`--optimizer_args "weight_decay=0.01" "betas=(0.9,0.95)"`ïŒã
## Custom LR Scheduler / ã«ã¹ã¿ã LRã¹ã±ãžã¥ãŒã©
### Rex
The Rex scheduler was added in [PR #513](https://github.com/kohya-ss/musubi-tuner/pull/513). It is based on the paper [REX: Revisiting Budgeted Training with an Improved Schedule](https://arxiv.org/abs/2107.04197), and the implementation is based on the repository by [IvanVassi](https://github.com/IvanVassi/REX_LR).
It has two parameters, `rex_alpha` and `rex_beta`, with default values of 0.1 and 0.9, respectively. These parameters are based on the defaults in IvanVassi's repository. The values proposed in the paper are 0.5 and 0.5. You can also use `--lr_warmup_steps` (default is 0) and `--lr_scheduler_min_lr_ratio` (default is 0.01).
It is similar to the Polynomial Scheduler with power less than 1, but Rex has a more gradual decrease in learning rate. For the specific LR curve, refer to the explanation in PR #513.
It is enabled by specifying `--lr_scheduler rex`. You can specify the parameters with `--lr_scheduler_args`.
```bash
--lr_scheduler rex --lr_scheduler_args "rex_alpha=0.1" "rex_beta=0.9"
```
æ¥æ¬èª
Rexã¹ã±ãžã¥ãŒã©ã¯ [PR #513](https://github.com/kohya-ss/musubi-tuner/pull/513) ã§è¿œå ãããŸãããè«æ [REX: Revisiting Budgeted Training with an Improved Schedule](https://arxiv.org/abs/2107.04197) ã«åºã¥ããŠãããã®ã§ãå®è£
㯠[IvanVassi](https://github.com/IvanVassi/REX_LR) æ°ã®ãªããžããªãå
ã«ããŠããŸãã
`rex_alpha`ãš`rex_beta`ã®2ã€ã®ãã©ã¡ãŒã¿ãæã¡ãããã©ã«ãå€ã¯ãããã0.1ãš0.9ã§ãããããã®ãã©ã¡ãŒã¿ã¯IvanVassiæ°ã®ãªããžããªã®ããã©ã«ãå€ã«åºã¥ããŠããŸããè«æã§æå±ãããŠããå€ã¯ãããã0.5ïŒ0.5ã§ãããŸãã`--lr_warmup_steps` ïŒããã©ã«ãå€ã¯0ïŒããã³ `--lr_scheduler_min_lr_ratio` ïŒããã©ã«ãå€ã¯0.01ïŒã䜿çšã§ããŸãã
powerã1æªæºã«èšå®ãã Polynomial Scheduler ã«äŒŒãŠããŸãããRexã¯åŠç¿çã®æžå°ãããç·©ããã§ããå
·äœçãªLRã®ã«ãŒãã¯PR #513ã®èª¬æãåç
§ããŠãã ããã
`--lr_scheduler rex`ãæå®ããããšã§æå¹ã«ãªããŸãã`--lr_scheduler_args`ã§ãã©ã¡ãŒã¿ãæå®ã§ããŸãã
```bash
--lr_scheduler rex --lr_scheduler_args "rex_alpha=0.1" "rex_beta=0.9"
```