Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable ipex optimization on textual-inversion #6349

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions examples/textual_inversion/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ to a number larger than one, *e.g.*:
--num_vectors 5
```

**CPU**: If you run on Intel Gen 4th Xeon (and later), use ipex and bf16 will get a significant acceleration.
You need to add `--mixed_precision="bf16"` and `--use_ipex` in the command and install the following package:
```
pip install intel-extension-for-pytorch
```

The saved textual inversion vectors will then be larger in size compared to the default case.

### Inference
Expand Down
15 changes: 14 additions & 1 deletion examples/textual_inversion/textual_inversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,14 @@ def parse_args():
help=(
"Whether to use mixed precision. Choose"
"between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10."
"and an Nvidia Ampere GPU."
"and an Intel Gen 4th Xeon (and later) or Nvidia Ampere GPU."
),
)
parser.add_argument(
"--use_ipex",
action="store_true",
help=(
"Whether or not to use ipex to accelerate the training process," "requires Intel Gen 3rd Xeon (and later)"
),
)
parser.add_argument(
Expand Down Expand Up @@ -779,6 +786,12 @@ def main():
unet.to(accelerator.device, dtype=weight_dtype)
vae.to(accelerator.device, dtype=weight_dtype)

if args.use_ipex:
import intel_extension_for_pytorch as ipex

unet = ipex.optimize(unet, dtype=weight_dtype)
vae = ipex.optimize(vae, dtype=weight_dtype)

# We need to recalculate our total training steps as the size of the training dataloader may have changed.
num_update_steps_per_epoch = math.ceil(len(train_dataloader) / args.gradient_accumulation_steps)
if overrode_max_train_steps:
Expand Down
Loading