Skip to content

Commit 8b84f85

Browse files
authored
[examples] fix mixed_precision arg (huggingface#1359)
* use accelerator to check mixed_precision * default `mixed_precision` to `None` * pass mixed_precision to accelerate launch
1 parent e50c25d commit 8b84f85

File tree

4 files changed

+16
-19
lines changed

4 files changed

+16
-19
lines changed

examples/dreambooth/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export INSTANCE_DIR="path-to-instance-images"
141141
export CLASS_DIR="path-to-class-images"
142142
export OUTPUT_DIR="path-to-save-model"
143143

144-
accelerate launch train_dreambooth.py \
144+
accelerate launch --mixed_precision="fp16" train_dreambooth.py \
145145
--pretrained_model_name_or_path=$MODEL_NAME \
146146
--instance_data_dir=$INSTANCE_DIR \
147147
--class_data_dir=$CLASS_DIR \
@@ -157,8 +157,7 @@ accelerate launch train_dreambooth.py \
157157
--lr_scheduler="constant" \
158158
--lr_warmup_steps=0 \
159159
--num_class_images=200 \
160-
--max_train_steps=800 \
161-
--mixed_precision=fp16
160+
--max_train_steps=800
162161
```
163162

164163
### Fine-tune text encoder with the UNet.

examples/dreambooth/train_dreambooth.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,12 @@ def parse_args(input_args=None):
187187
parser.add_argument(
188188
"--mixed_precision",
189189
type=str,
190-
default="no",
190+
default=None,
191191
choices=["no", "fp16", "bf16"],
192192
help=(
193-
"Whether to use mixed precision. Choose"
194-
"between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10."
195-
"and an Nvidia Ampere GPU."
193+
"Whether to use mixed precision. Choose between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >="
194+
" 1.10.and an Nvidia Ampere GPU. Default to the value of accelerate config of the current system or the"
195+
" flag passed with the `accelerate.launch` command. Use this argument to override the accelerate config."
196196
),
197197
)
198198
parser.add_argument("--local_rank", type=int, default=-1, help="For distributed training: local_rank")
@@ -538,9 +538,9 @@ def collate_fn(examples):
538538
)
539539

540540
weight_dtype = torch.float32
541-
if args.mixed_precision == "fp16":
541+
if accelerator.mixed_precision == "fp16":
542542
weight_dtype = torch.float16
543-
elif args.mixed_precision == "bf16":
543+
elif accelerator.mixed_precision == "bf16":
544544
weight_dtype = torch.bfloat16
545545

546546
# Move text_encode and vae to gpu.

examples/text_to_image/README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,14 @@ With `gradient_checkpointing` and `mixed_precision` it should be possible to fin
4646
export MODEL_NAME="CompVis/stable-diffusion-v1-4"
4747
export dataset_name="lambdalabs/pokemon-blip-captions"
4848

49-
accelerate launch train_text_to_image.py \
49+
accelerate launch --mixed_precision="fp16" train_text_to_image.py \
5050
--pretrained_model_name_or_path=$MODEL_NAME \
5151
--dataset_name=$dataset_name \
5252
--use_ema \
5353
--resolution=512 --center_crop --random_flip \
5454
--train_batch_size=1 \
5555
--gradient_accumulation_steps=4 \
5656
--gradient_checkpointing \
57-
--mixed_precision="fp16" \
5857
--max_train_steps=15000 \
5958
--learning_rate=1e-05 \
6059
--max_grad_norm=1 \
@@ -70,15 +69,14 @@ If you wish to use custom loading logic, you should modify the script, we have l
7069
export MODEL_NAME="CompVis/stable-diffusion-v1-4"
7170
export TRAIN_DIR="path_to_your_dataset"
7271

73-
accelerate launch train_text_to_image.py \
72+
accelerate launch --mixed_precision="fp16" train_text_to_image.py \
7473
--pretrained_model_name_or_path=$MODEL_NAME \
7574
--train_data_dir=$TRAIN_DIR \
7675
--use_ema \
7776
--resolution=512 --center_crop --random_flip \
7877
--train_batch_size=1 \
7978
--gradient_accumulation_steps=4 \
8079
--gradient_checkpointing \
81-
--mixed_precision="fp16" \
8280
--max_train_steps=15000 \
8381
--learning_rate=1e-05 \
8482
--max_grad_norm=1 \

examples/text_to_image/train_text_to_image.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,12 @@ def parse_args():
186186
parser.add_argument(
187187
"--mixed_precision",
188188
type=str,
189-
default="no",
189+
default=None,
190190
choices=["no", "fp16", "bf16"],
191191
help=(
192-
"Whether to use mixed precision. Choose"
193-
"between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10."
194-
"and an Nvidia Ampere GPU."
192+
"Whether to use mixed precision. Choose between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >="
193+
" 1.10.and an Nvidia Ampere GPU. Default to the value of accelerate config of the current system or the"
194+
" flag passed with the `accelerate.launch` command. Use this argument to override the accelerate config."
195195
),
196196
)
197197
parser.add_argument(
@@ -496,9 +496,9 @@ def collate_fn(examples):
496496
)
497497

498498
weight_dtype = torch.float32
499-
if args.mixed_precision == "fp16":
499+
if accelerator.mixed_precision == "fp16":
500500
weight_dtype = torch.float16
501-
elif args.mixed_precision == "bf16":
501+
elif accelerator.mixed_precision == "bf16":
502502
weight_dtype = torch.bfloat16
503503

504504
# Move text_encode and vae to gpu.

0 commit comments

Comments
 (0)