|
| 1 | +## Textual Inversion fine-tuning example |
| 2 | + |
| 3 | +[Textual inversion](https://arxiv.org/abs/2208.01618) is a method to personalize text2image models like stable diffusion on your own images using just 3-5 examples. |
| 4 | +The `textual_inversion.py` script shows how to implement the training procedure and adapt it for stable diffusion. |
| 5 | + |
| 6 | +## Training with Intel Extension for PyTorch |
| 7 | + |
| 8 | +Intel Extension for PyTorch provides the optimizations for faster training and inference on CPUs. You can leverage the training example "textual_inversion.py". Follow the [instructions](https://github.com/huggingface/diffusers/tree/main/examples/textual_inversion) to get the model and [dataset](https://huggingface.co/sd-concepts-library/dicoo2) before running the script. |
| 9 | + |
| 10 | +The example supports both single node and multi-node distributed training: |
| 11 | + |
| 12 | +### Single node training |
| 13 | + |
| 14 | +```bash |
| 15 | +export MODEL_NAME="CompVis/stable-diffusion-v1-4" |
| 16 | +export DATA_DIR="path-to-dir-containing-dicoo-images" |
| 17 | + |
| 18 | +python textual_inversion.py \ |
| 19 | + --pretrained_model_name_or_path=$MODEL_NAME \ |
| 20 | + --train_data_dir=$DATA_DIR \ |
| 21 | + --learnable_property="object" \ |
| 22 | + --placeholder_token="<dicoo>" --initializer_token="toy" \ |
| 23 | + --seed=7 \ |
| 24 | + --resolution=512 \ |
| 25 | + --train_batch_size=1 \ |
| 26 | + --gradient_accumulation_steps=1 \ |
| 27 | + --max_train_steps=3000 \ |
| 28 | + --learning_rate=2.5e-03 --scale_lr \ |
| 29 | + --output_dir="textual_inversion_dicoo" |
| 30 | +``` |
| 31 | + |
| 32 | +Note: Bfloat16 is available on Intel Xeon Scalable Processors Cooper Lake or Sapphire Rapids. You may not get performance speedup without Bfloat16 support. |
| 33 | + |
| 34 | +### Multi-node distributed training |
| 35 | + |
| 36 | +Before running the scripts, make sure to install the library's training dependencies successfully: |
| 37 | + |
| 38 | +```bash |
| 39 | +python -m pip install oneccl_bind_pt==1.13 -f https://developer.intel.com/ipex-whl-stable-cpu |
| 40 | +``` |
| 41 | + |
| 42 | +```bash |
| 43 | +export MODEL_NAME="CompVis/stable-diffusion-v1-4" |
| 44 | +export DATA_DIR="path-to-dir-containing-dicoo-images" |
| 45 | + |
| 46 | +oneccl_bindings_for_pytorch_path=$(python -c "from oneccl_bindings_for_pytorch import cwd; print(cwd)") |
| 47 | +source $oneccl_bindings_for_pytorch_path/env/setvars.sh |
| 48 | + |
| 49 | +python -m intel_extension_for_pytorch.cpu.launch --distributed \ |
| 50 | + --hostfile hostfile --nnodes 2 --nproc_per_node 2 textual_inversion.py \ |
| 51 | + --pretrained_model_name_or_path=$MODEL_NAME \ |
| 52 | + --train_data_dir=$DATA_DIR \ |
| 53 | + --learnable_property="object" \ |
| 54 | + --placeholder_token="<dicoo>" --initializer_token="toy" \ |
| 55 | + --seed=7 \ |
| 56 | + --resolution=512 \ |
| 57 | + --train_batch_size=1 \ |
| 58 | + --gradient_accumulation_steps=1 \ |
| 59 | + --max_train_steps=750 \ |
| 60 | + --learning_rate=2.5e-03 --scale_lr \ |
| 61 | + --output_dir="textual_inversion_dicoo" |
| 62 | +``` |
| 63 | +The above is a simple distributed training usage on 2 nodes with 2 processes on each node. Add the right hostname or ip address in the "hostfile" and make sure these 2 nodes are reachable from each other. For more details, please refer to the [user guide](https://github.com/intel/torch-ccl). |
| 64 | + |
| 65 | + |
| 66 | +### Reference |
| 67 | + |
| 68 | +We publish a [Medium blog](https://medium.com/intel-analytics-software/personalized-stable-diffusion-with-few-shot-fine-tuning-on-a-single-cpu-f01a3316b13) on how to create your own Stable Diffusion model on CPUs using textual inversion. Try it out now, if you have interests. |
0 commit comments