{"id":13488217,"url":"https://github.com/mkshing/ziplora-pytorch","last_synced_at":"2026-01-17T00:57:23.853Z","repository":{"id":208860874,"uuid":"722411111","full_name":"mkshing/ziplora-pytorch","owner":"mkshing","description":"Implementation of \"ZipLoRA: Any Subject in Any Style by Effectively Merging LoRAs\"","archived":false,"fork":false,"pushed_at":"2023-12-27T14:29:58.000Z","size":6210,"stargazers_count":513,"open_issues_count":15,"forks_count":35,"subscribers_count":11,"default_branch":"main","last_synced_at":"2024-10-30T23:36:28.634Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mkshing.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-23T04:55:34.000Z","updated_at":"2024-10-30T17:07:22.000Z","dependencies_parsed_at":"2024-10-30T23:42:40.442Z","dependency_job_id":null,"html_url":"https://github.com/mkshing/ziplora-pytorch","commit_stats":null,"previous_names":["mkshing/ziplora-pytorch"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mkshing%2Fziplora-pytorch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mkshing%2Fziplora-pytorch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mkshing%2Fziplora-pytorch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mkshing%2Fziplora-pytorch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mkshing","download_url":"https://codeload.github.com/mkshing/ziplora-pytorch/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245944028,"owners_count":20697946,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T18:01:11.637Z","updated_at":"2026-01-17T00:57:23.839Z","avatar_url":"https://github.com/mkshing.png","language":"Python","funding_links":[],"categories":["New Concept Learning","Python"],"sub_categories":[],"readme":"# ZipLoRA-pytorch\nThis is an implementation of [ZipLoRA: Any Subject in Any Style by Effectively Merging LoRAs](https://ziplora.github.io/) by [mkshing](https://twitter.com/mk1stats).\n\nThe paper summary by the author is found [here](https://twitter.com/natanielruizg/status/1727718489425616912).\n\n![result](assets/result.png)\n\n## Installation\n```\ngit clone git@github.com:mkshing/ziplora-pytorch.git\ncd ziplora-pytorch\npip install -r requirements.txt\n```\n\n## Usage\n\n### 1. Train LoRAs for subject/style images\nIn this step, 2 LoRAs for subject/style images are trained based on SDXL. Using SDXL here is important because they found that the pre-trained SDXL exhibits strong learning when fine-tuned on only one reference style image.\n\nFortunately, diffusers already implemented LoRA based on SDXL [here](https://github.com/huggingface/diffusers/blob/main/examples/dreambooth/README_sdxl.md) and you can simply follow the instruction. \n\nFor example, your training script would be like this.\n```bash\nexport MODEL_NAME=\"stabilityai/stable-diffusion-xl-base-1.0\"\n# for subject\nexport OUTPUT_DIR=\"lora-sdxl-dog\"\nexport INSTANCE_DIR=\"dog\"\nexport PROMPT=\"a sbu dog\"\nexport VALID_PROMPT=\"a sbu dog in a bucket\"\n\n# for style\n# export OUTPUT_DIR=\"lora-sdxl-waterpainting\"\n# export INSTANCE_DIR=\"waterpainting\"\n# export PROMPT=\"a cat of in szn style\"\n# export VALID_PROMPT=\"a man in szn style\"\n\naccelerate launch train_dreambooth_lora_sdxl.py \\\n  --pretrained_model_name_or_path=$MODEL_NAME  \\\n  --instance_data_dir=$INSTANCE_DIR \\\n  --output_dir=$OUTPUT_DIR \\\n  --instance_prompt=\"${PROMPT}\" \\\n  --rank=64 \\\n  --resolution=1024 \\\n  --train_batch_size=1 \\\n  --learning_rate=5e-5 \\\n  --report_to=\"wandb\" \\\n  --lr_scheduler=\"constant\" \\\n  --lr_warmup_steps=0 \\\n  --max_train_steps=1000 \\\n  --validation_prompt=\"${VALID_PROMPT}\" \\\n  --validation_epochs=50 \\\n  --seed=\"0\" \\\n  --mixed_precision=\"fp16\" \\\n  --enable_xformers_memory_efficient_attention \\\n  --gradient_checkpointing \\\n  --use_8bit_adam \\\n  --push_to_hub \\\n```\n\n* In the above script, all hyperparameters such as `--max_train_steps` and `--rank` are followed the paper. But, of course, you can tweak them for your images. \n* You can find style images in [aim-uofa/StyleDrop-PyTorch](https://github.com/aim-uofa/StyleDrop-PyTorch/tree/main/data).\n\n### 2. Train ZipLoRA\n\n```bash\nexport MODEL_NAME=\"stabilityai/stable-diffusion-xl-base-1.0\"\n\n# for subject\nexport LORA_PATH=\"mkshing/lora-sdxl-dog\"\nexport INSTANCE_DIR=\"dog\"\nexport PROMPT=\"a sbu dog\"\n\n# for style\nexport LORA_PATH2=\"mkshing/lora-sdxl-waterpainting\"\nexport INSTANCE_DIR2=\"waterpainting\"\nexport PROMPT2=\"a cat of in szn style\"\n\n# general \nexport OUTPUT_DIR=\"ziplora-sdxl-dog-waterpainting\"\nexport VALID_PROMPT=\"a sbu dog in szn style\"\n\n\naccelerate launch train_dreambooth_ziplora_sdxl.py \\\n  --pretrained_model_name_or_path=$MODEL_NAME  \\\n  --output_dir=$OUTPUT_DIR \\\n  --lora_name_or_path=$LORA_PATH \\\n  --instance_prompt=\"${PROMPT}\" \\\n  --instance_data_dir=$INSTANCE_DIR \\\n  --lora_name_or_path_2=$LORA_PATH2 \\\n  --instance_prompt_2=\"${PROMPT2}\" \\\n  --instance_data_dir_2=$INSTANCE_DIR2 \\\n  --resolution=1024 \\\n  --train_batch_size=1 \\\n  --learning_rate=5e-5 \\\n  --similarity_lambda=0.01 \\\n  --lr_scheduler=\"constant\" \\\n  --lr_warmup_steps=0 \\\n  --max_train_steps=100 \\\n  --validation_prompt=\"${VALID_PROMPT}\" \\\n  --validation_epochs=10 \\\n  --seed=\"0\" \\\n  --mixed_precision=\"fp16\" \\\n  --report_to=\"wandb\" \\\n  --gradient_checkpointing \\\n  --use_8bit_adam \\\n  --enable_xformers_memory_efficient_attention \\\n\n```\n\n* If you're facing VRAM limitations during training, use the `--quick_release` flag to help free up VRAM.\n\n### 3. Inference\n\n```python\nimport torch\nfrom diffusers import StableDiffusionXLPipeline\nfrom ziplora_pytorch.utils import insert_ziplora_to_unet\n\npipeline = StableDiffusionXLPipeline.from_pretrained(pretrained_model_name_or_path)\npipeline.unet = insert_ziplora_to_unet(pipeline.unet, ziplora_name_or_path)\npipeline.to(device=\"cuda\", dtype=torch.float16)\nimage = pipeline(prompt=prompt).images[0]\nimage.save(\"out.png\")\n```\n\nAlso, you can quickly interact with your ziplora by using gradio.\n```bash\nexport MODEL_NAME=\"stabilityai/stable-diffusion-xl-base-1.0\"\nexport ZIPLORA_PATH=\"...\"\n\npython inference.py --pretrained_model_name_or_path=$MODEL_NAME --ziplora_name_or_path=$ZIPLORA_PATH \n```\n\n\n## TODO\n\n- [x] super quick instruction for training each loras\n- [x] ZipLoRA (training)\n- [x] ZipLoRA (inference)\n- [ ] Pre-optimization lora weights \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmkshing%2Fziplora-pytorch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmkshing%2Fziplora-pytorch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmkshing%2Fziplora-pytorch/lists"}