mirror of
https://github.com/ostris/ai-toolkit.git
synced 2026-01-26 16:39:47 +00:00
346 lines
25 KiB
Markdown
346 lines
25 KiB
Markdown
# AI Toolkit by Ostris
|
||
|
||
## Support My Work
|
||
|
||
If you enjoy my work, or use it for commercial purposes, please consider sponsoring me so I can continue to maintain it. Every bit helps!
|
||
|
||
[Become a sponsor on GitHub](https://github.com/orgs/ostris) or [support me on Patreon](https://www.patreon.com/ostris).
|
||
|
||
Thank you to all my current supporters!
|
||
|
||
_Last updated: 2025-03-20_
|
||
|
||
### GitHub Sponsors
|
||
|
||
<a href="https://github.com/replicate" title="Replicate"><img src="https://avatars.githubusercontent.com/u/60410876?v=4" width="50" height="50" alt="Replicate" style="border-radius:50%"></a> <a href="https://github.com/weights-ai" title="Weights"><img src="https://avatars.githubusercontent.com/u/185568492?v=4" width="50" height="50" alt="Weights" style="border-radius:50%"></a>
|
||
|
||
### Patreon Supporters
|
||
|
||
<a href="None" title="Abraham Irawan"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/60995694/92e0e8f336eb4a5bb8d99b940247d1d1/eyJ3IjoyMDB9/1.png?token-time=2145916800&token-hash=pj6Tm8XRdpGJcAEdnCakqYSNiSjoAYjvZescX7d0ic0%3D" width="50" height="50" alt="Abraham Irawan" style="border-radius:50%"></a> <a href="None" title="Al H"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/570742/4ceb33453a5a4745b430a216aba9280f/eyJ3IjoyMDB9/1.jpg?token-time=2145916800&token-hash=wUzsI5cO5Evp2ukIGdSgBbvKeYgv5LSOQMa6Br33Rrs%3D" width="50" height="50" alt="Al H" style="border-radius:50%"></a> <a href="None" title="Armin Behjati"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/93348210/5c650f32a0bc481d80900d2674528777/eyJ3IjoyMDB9/1.jpeg?token-time=2145916800&token-hash=PpXK9B_iy288annlNdLOexhiQHbTftPEDeCh-sTQ2KA%3D" width="50" height="50" alt="Armin Behjati" style="border-radius:50%"></a> <a href="None" title="Austin Robinson"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/164958178/4eb7a37baa0541bab7a091f2b14615b7/eyJ3IjoyMDB9/1.png?token-time=2145916800&token-hash=_aaum7fBJAGaJhMBhlR8vqYavDhExdVxmO9mwd3_XMw%3D" width="50" height="50" alt="Austin Robinson" style="border-radius:50%"></a> <a href="None" title="Bharat Prabhakar"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/134129880/680c7e14cd1a4d1a9face921fb010f88/eyJ3IjoyMDB9/1.png?token-time=2145916800&token-hash=vNKojv67krNqx7gdpKBX1R_stX2TkMRYvRc0xZrbY6s%3D" width="50" height="50" alt="Bharat Prabhakar" style="border-radius:50%"></a> <a href="None" title="clement Delangue"><img src="https://c8.patreon.com/3/200/33158543" width="50" height="50" alt="clement Delangue" style="border-radius:50%"></a> <a href="None" title="Cosmosis"><img src="https://c8.patreon.com/3/200/70218846" width="50" height="50" alt="Cosmosis" style="border-radius:50%"></a> <a href="None" title="David Garrido"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/27288932/6c35d2d961ee4e14a7a368c990791315/eyJ3IjoyMDB9/1.jpeg?token-time=2145916800&token-hash=dpFFssZXZM_KZMKQhl3uDwwusdFw1c_v9x_ChJU7_zc%3D" width="50" height="50" alt="David Garrido" style="border-radius:50%"></a> <a href="None" title="Doron Adler"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/82763/f99cc484361d4b9d94fe4f0814ada303/eyJ3IjoyMDB9/1.jpeg?token-time=2145916800&token-hash=BpwC020pR3TRZ4r0RSCiSIOh-jmatkrpy1h2XU4sGa4%3D" width="50" height="50" alt="Doron Adler" style="border-radius:50%"></a> <a href="None" title="Eli Slugworth"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/54890369/45cea21d82974c78bf43956de7fb0e12/eyJ3IjoyMDB9/2.jpeg?token-time=2145916800&token-hash=IK6OT6UpusHgdaC4y8IhK5XxXiP5TuLy3vjvgL77Fho%3D" width="50" height="50" alt="Eli Slugworth" style="border-radius:50%"></a> <a href="None" title="EmmanuelMr18"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/98811435/3a3632d1795b4c2b9f8f0270f2f6a650/eyJ3IjoyMDB9/1.jpeg?token-time=2145916800&token-hash=93w8RMxwXlcM4X74t03u6P5_SrKvlm1IpjnD2SzVpJk%3D" width="50" height="50" alt="EmmanuelMr18" style="border-radius:50%"></a> <a href="None" title="Gili Ben Shahar"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/83054970/13de6cb103ad41a5841edf549e66cd51/eyJ3IjoyMDB9/1.jpeg?token-time=2145916800&token-hash=wU_Eke9VYcfI40FAQvdEV84Xspqlo5VSiafLqhg_FOE%3D" width="50" height="50" alt="Gili Ben Shahar" style="border-radius:50%"></a> <a href="None" title="HestoySeghuro ."><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/30931983/54ab4e4ceab946e79a6418d205f9ed51/eyJ3IjoyMDB9/1.png?token-time=2145916800&token-hash=LBmsSsMQZhO6yRZ_YyRwTgE6a7BVWrGNsAVveLXHXR0%3D" width="50" height="50" alt="HestoySeghuro ." style="border-radius:50%"></a> <a href="None" title="Jack Blakely"><img src="https://c8.patreon.com/3/200/4105384" width="50" height="50" alt="Jack Blakely" style="border-radius:50%"></a> <a href="None" title="Jack English"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/45562978/0de33cf52ec642ae8a2f612cddec4ca6/eyJ3IjoyMDB9/1.jpeg?token-time=2145916800&token-hash=hSAvaD4phiLcF0pvX7FP0juI5NQWCon-_TZSNpJzQJg%3D" width="50" height="50" alt="Jack English" style="border-radius:50%"></a> <a href="None" title="Jason"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/150257013/0e9e333d30294eef9f4d6821166966d8/eyJ3IjoyMDB9/2.png?token-time=2145916800&token-hash=hPH_rp5L5OJ9ZMS1wZfpVXDB4lRv2GHpV6r8Jmbmqww%3D" width="50" height="50" alt="Jason" style="border-radius:50%"></a> <a href="None" title="Jean-Tristan Marin"><img src="https://c8.patreon.com/3/200/27791680" width="50" height="50" alt="Jean-Tristan Marin" style="border-radius:50%"></a> <a href="None" title="Jodh Singh"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/131773947/eda3405aa582437db4582fce908c8739/eyJ3IjoyMDB9/1.png?token-time=2145916800&token-hash=S4Bh0sMqTNmJlo3uRr7co5d_kxvBjITemDTfi_1KrCA%3D" width="50" height="50" alt="Jodh Singh" style="border-radius:50%"></a> <a href="None" title="John Dopamine"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/103077711/bb215761cc004e80bd9cec7d4bcd636d/eyJ3IjoyMDB9/2.jpeg?token-time=2145916800&token-hash=zvtBie29rRTKTXvAA2KhOI-l3mSMk9xxr-mg_CksLtc%3D" width="50" height="50" alt="John Dopamine" style="border-radius:50%"></a> <a href="None" title="Joseph Rocca"><img src="https://c8.patreon.com/3/200/93304" width="50" height="50" alt="Joseph Rocca" style="border-radius:50%"></a> <a href="None" title="Kasım Açıkbaş"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/31471379/0a887513ee314a1c86d0b6f8792e9795/eyJ3IjoyMDB9/1.jpeg?token-time=2145916800&token-hash=DJMZs3rDlS0fCM_ahm95FAbjleM_L0gsO9qAPzqd0nA%3D" width="50" height="50" alt="Kasım Açıkbaş" style="border-radius:50%"></a> <a href="None" title="Kristjan Retter"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/152118848/3b15a43d71714552b5ed1c9f84e66adf/eyJ3IjoyMDB9/1.png?token-time=2145916800&token-hash=IEKE18CBHVZ3k-08UD7Dkb7HbiFHb84W0FATdLMI0Dg%3D" width="50" height="50" alt="Kristjan Retter" style="border-radius:50%"></a> <a href="None" title="Michael Levine"><img src="https://c8.patreon.com/3/200/22809690" width="50" height="50" alt="Michael Levine" style="border-radius:50%"></a> <a href="None" title="Miguel Lara"><img src="https://c8.patreon.com/3/200/83319230" width="50" height="50" alt="Miguel Lara" style="border-radius:50%"></a> <a href="None" title="Misch Strotz"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/8654302/b0f5ebedc62a47c4b56222693e1254e9/eyJ3IjoyMDB9/1.jpeg?token-time=2145916800&token-hash=lpeicIh1_S-3Ji3W27gyiRB7iXurp8Bx8HAzDHftOuo%3D" width="50" height="50" alt="Misch Strotz" style="border-radius:50%"></a> <a href="None" title="Mohamed Oumoumad"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/2298192/1228b69bd7d7481baf3103315183250d/eyJ3IjoyMDB9/1.jpg?token-time=2145916800&token-hash=1B7dbXy_gAcPT9WXBesLhs7z_9APiz2k1Wx4Vml_-8Q%3D" width="50" height="50" alt="Mohamed Oumoumad" style="border-radius:50%"></a> <a href="None" title="Noctre"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/99036356/7ae9c4d80e604e739b68cca12ee2ed01/eyJ3IjoyMDB9/3.png?token-time=2145916800&token-hash=zK0dHe6A937WtNlrGdefoXFTPPzHUCfn__23HP8-Ui0%3D" width="50" height="50" alt="Noctre" style="border-radius:50%"></a> <a href="None" title="Patron"><img src="https://c8.patreon.com/3/200/8449560" width="50" height="50" alt="Patron" style="border-radius:50%"></a> <a href="None" title="Paul Fidika"><img src="https://c8.patreon.com/3/200/24440789" width="50" height="50" alt="Paul Fidika" style="border-radius:50%"></a> <a href="None" title="Plaidam"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/338551/e8f257d8d3dd46c38272b391a5785948/eyJ3IjoyMDB9/1.jpg?token-time=2145916800&token-hash=GLom1rGgOZjBeO7I1OnjiIgWmjl6PO9ZjBB8YTvc7AM%3D" width="50" height="50" alt="Plaidam" style="border-radius:50%"></a> <a href="None" title="Prasanth Veerina"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/162524101/81a72689c3754ac5b9e38612ce5ce914/eyJ3IjoyMDB9/1.png?token-time=2145916800&token-hash=3XLSlLFCWAQ-0wd2_vZMikyotdQNSzKOjoyeoJiZEw0%3D" width="50" height="50" alt="Prasanth Veerina" style="border-radius:50%"></a> <a href="None" title="RayHell"><img src="https://c8.patreon.com/3/200/24653779" width="50" height="50" alt="RayHell" style="border-radius:50%"></a> <a href="None" title="Razvan Grigore"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/156382802/a547e1a8952144ba94f301b10ea37f7a/eyJ3IjoyMDB9/1.jpeg?token-time=2145916800&token-hash=4DoecSYwQGqf6LgYUCHKcEiKg2p3Rj_3BmenPkH3EK8%3D" width="50" height="50" alt="Razvan Grigore" style="border-radius:50%"></a> <a href="None" title="Steve Hanff"><img src="https://c8.patreon.com/3/200/548524" width="50" height="50" alt="Steve Hanff" style="border-radius:50%"></a> <a href="None" title="Steven Simmons"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/163426977/fc3941c79e894fef985d9f5440255313/eyJ3IjoyMDB9/1.png?token-time=2145916800&token-hash=TjwllfKCd_Ftt1C2wFYdcOdJZxyuPaRpEbKjrfzk0Zw%3D" width="50" height="50" alt="Steven Simmons" style="border-radius:50%"></a> <a href="None" title="Sören "><img src="https://c8.patreon.com/3/200/4541423" width="50" height="50" alt="Sören " style="border-radius:50%"></a> <a href="None" title="The Local Lab"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/141098579/1a9f0a1249d447a7a0df718a57343912/eyJ3IjoyMDB9/2.png?token-time=2145916800&token-hash=Rd_AjZGhMATVkZDf8E95ILc0n93gvvFWe1Ig0_dxwf4%3D" width="50" height="50" alt="The Local Lab" style="border-radius:50%"></a> <a href="None" title="Trent Hunter"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/31950857/c567dc648f6144be9f6234946df05da2/eyJ3IjoyMDB9/1.jpeg?token-time=2145916800&token-hash=3Vx4R1eOfD4X_ZPPd40MsZ-3lyknLM35XmaHRELnWjM%3D" width="50" height="50" alt="Trent Hunter" style="border-radius:50%"></a> <a href="None" title="Un Defined"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/155963250/6f8fd7075c3b4247bfeb054ba49172d6/eyJ3IjoyMDB9/1.png?token-time=2145916800&token-hash=twmKs4mADF_h7bKh5jBuigYVScMeaeHv2pEPin9K0Dg%3D" width="50" height="50" alt="Un Defined" style="border-radius:50%"></a> <a href="None" title="Vladimir Sotnikov"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/161471720/dd330b4036d44a5985ed5985c12a5def/eyJ3IjoyMDB9/1.jpeg?token-time=2145916800&token-hash=qkRvrEc5gLPxaXxLvcvbYv1W1lcmOoTwhj4A9Cq5BxQ%3D" width="50" height="50" alt="Vladimir Sotnikov" style="border-radius:50%"></a> <a href="None" title="Wesley Reitzfeld"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/110407414/30f9e9d88ef945ddb0f47fd23a8cbac2/eyJ3IjoyMDB9/1.jpeg?token-time=2145916800&token-hash=QQRWOkMyOfDBERHn4O8N2wMB32zeiIEsydVTbSNUw-I%3D" width="50" height="50" alt="Wesley Reitzfeld" style="border-radius:50%"></a> <a href="None" title="Zoltán-Csaba Nyiró"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/162398691/89d78d89eecb4d6b981ce8c3c6a3d4b8/eyJ3IjoyMDB9/1.png?token-time=2145916800&token-hash=SWhI-0jGpY6Nc_bUQeXz4pa9DRURi9VnnnJ3Mxjg1po%3D" width="50" height="50" alt="Zoltán-Csaba Nyiró" style="border-radius:50%"></a> <a href="None" title="Алексей Наумов"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/26019082/6ba968129e284c869069b261c875ae02/eyJ3IjoyMDB9/1.png?token-time=2145916800&token-hash=Jz-Kk9l8RIvGMNcaGXuN8_vaY3G435lFmtJtFZA3OCs%3D" width="50" height="50" alt="Алексей Наумов" style="border-radius:50%"></a> <a href="None" title="עומר מכלוף"><img src="https://c10.patreonusercontent.com/4/patreon-media/p/user/97985240/3d1d0e6905d045aba713e8132cab4a30/eyJ3IjoyMDB9/1.png?token-time=2145916800&token-hash=pG3X2m-py2lRYI2aoJiXI47_4ArD78ZHdSm6jCAHA_w%3D" width="50" height="50" alt="עומר מכלוף" style="border-radius:50%"></a>
|
||
|
||
|
||
---
|
||
|
||
|
||
|
||
|
||
## Installation
|
||
|
||
Requirements:
|
||
- python >3.10
|
||
- Nvidia GPU with enough ram to do what you need
|
||
- python venv
|
||
- git
|
||
|
||
|
||
Linux:
|
||
```bash
|
||
git clone https://github.com/ostris/ai-toolkit.git
|
||
cd ai-toolkit
|
||
git submodule update --init --recursive
|
||
python3 -m venv venv
|
||
source venv/bin/activate
|
||
# .\venv\Scripts\activate on windows
|
||
# install torch first
|
||
pip3 install torch
|
||
pip3 install -r requirements.txt
|
||
```
|
||
|
||
Windows:
|
||
```bash
|
||
git clone https://github.com/ostris/ai-toolkit.git
|
||
cd ai-toolkit
|
||
git submodule update --init --recursive
|
||
python -m venv venv
|
||
.\venv\Scripts\activate
|
||
pip install torch==2.5.1 torchvision==0.20.1 --index-url https://download.pytorch.org/whl/cu124
|
||
pip install -r requirements.txt
|
||
```
|
||
|
||
|
||
# AI Toolkit UI
|
||
|
||
<img src="https://ostris.com/wp-content/uploads/2025/02/toolkit-ui.jpg" alt="AI Toolkit UI" width="100%">
|
||
|
||
The AI Toolkit UI is a web interface for the AI Toolkit. It allows you to easily start, stop, and monitor jobs. It also allows you to easily train models with a few clicks. It also allows you to set a token for the UI to prevent unauthorized access so it is mostly safe to run on an exposed server.
|
||
|
||
## Running the UI
|
||
|
||
Requirements:
|
||
- Node.js > 18
|
||
|
||
The UI does not need to be kept running for the jobs to run. It is only needed to start/stop/monitor jobs. The commands below
|
||
will install / update the UI and it's dependencies and start the UI.
|
||
|
||
```bash
|
||
cd ui
|
||
npm run build_and_start
|
||
```
|
||
|
||
You can now access the UI at `http://localhost:8675` or `http://<your-ip>:8675` if you are running it on a server.
|
||
|
||
## Securing the UI
|
||
|
||
If you are hosting the UI on a cloud provider or any network that is not secure, I highly recommend securing it with an auth token.
|
||
You can do this by setting the environment variable `AI_TOOLKIT_AUTH` to super secure password. This token will be required to access
|
||
the UI. You can set this when starting the UI like so:
|
||
|
||
```bash
|
||
# Linux
|
||
AI_TOOLKIT_AUTH=super_secure_password npm run build_and_start
|
||
|
||
# Windows
|
||
set AI_TOOLKIT_AUTH=super_secure_password && npm run build_and_start
|
||
|
||
# Windows Powershell
|
||
$env:AI_TOOLKIT_AUTH="super_secure_password"; npm run build_and_start
|
||
```
|
||
|
||
|
||
## FLUX.1 Training
|
||
|
||
### Tutorial
|
||
|
||
To get started quickly, check out [@araminta_k](https://x.com/araminta_k) tutorial on [Finetuning Flux Dev on a 3090](https://www.youtube.com/watch?v=HzGW_Kyermg) with 24GB VRAM.
|
||
|
||
|
||
### Requirements
|
||
You currently need a GPU with **at least 24GB of VRAM** to train FLUX.1. If you are using it as your GPU to control
|
||
your monitors, you probably need to set the flag `low_vram: true` in the config file under `model:`. This will quantize
|
||
the model on CPU and should allow it to train with monitors attached. Users have gotten it to work on Windows with WSL,
|
||
but there are some reports of a bug when running on windows natively.
|
||
I have only tested on linux for now. This is still extremely experimental
|
||
and a lot of quantizing and tricks had to happen to get it to fit on 24GB at all.
|
||
|
||
### FLUX.1-dev
|
||
|
||
FLUX.1-dev has a non-commercial license. Which means anything you train will inherit the
|
||
non-commercial license. It is also a gated model, so you need to accept the license on HF before using it.
|
||
Otherwise, this will fail. Here are the required steps to setup a license.
|
||
|
||
1. Sign into HF and accept the model access here [black-forest-labs/FLUX.1-dev](https://huggingface.co/black-forest-labs/FLUX.1-dev)
|
||
2. Make a file named `.env` in the root on this folder
|
||
3. [Get a READ key from huggingface](https://huggingface.co/settings/tokens/new?) and add it to the `.env` file like so `HF_TOKEN=your_key_here`
|
||
|
||
### FLUX.1-schnell
|
||
|
||
FLUX.1-schnell is Apache 2.0. Anything trained on it can be licensed however you want and it does not require a HF_TOKEN to train.
|
||
However, it does require a special adapter to train with it, [ostris/FLUX.1-schnell-training-adapter](https://huggingface.co/ostris/FLUX.1-schnell-training-adapter).
|
||
It is also highly experimental. For best overall quality, training on FLUX.1-dev is recommended.
|
||
|
||
To use it, You just need to add the assistant to the `model` section of your config file like so:
|
||
|
||
```yaml
|
||
model:
|
||
name_or_path: "black-forest-labs/FLUX.1-schnell"
|
||
assistant_lora_path: "ostris/FLUX.1-schnell-training-adapter"
|
||
is_flux: true
|
||
quantize: true
|
||
```
|
||
|
||
You also need to adjust your sample steps since schnell does not require as many
|
||
|
||
```yaml
|
||
sample:
|
||
guidance_scale: 1 # schnell does not do guidance
|
||
sample_steps: 4 # 1 - 4 works well
|
||
```
|
||
|
||
### Training
|
||
1. Copy the example config file located at `config/examples/train_lora_flux_24gb.yaml` (`config/examples/train_lora_flux_schnell_24gb.yaml` for schnell) to the `config` folder and rename it to `whatever_you_want.yml`
|
||
2. Edit the file following the comments in the file
|
||
3. Run the file like so `python run.py config/whatever_you_want.yml`
|
||
|
||
A folder with the name and the training folder from the config file will be created when you start. It will have all
|
||
checkpoints and images in it. You can stop the training at any time using ctrl+c and when you resume, it will pick back up
|
||
from the last checkpoint.
|
||
|
||
IMPORTANT. If you press crtl+c while it is saving, it will likely corrupt that checkpoint. So wait until it is done saving
|
||
|
||
### Need help?
|
||
|
||
Please do not open a bug report unless it is a bug in the code. You are welcome to [Join my Discord](https://discord.gg/VXmU2f5WEU)
|
||
and ask for help there. However, please refrain from PMing me directly with general question or support. Ask in the discord
|
||
and I will answer when I can.
|
||
|
||
## Gradio UI
|
||
|
||
To get started training locally with a with a custom UI, once you followed the steps above and `ai-toolkit` is installed:
|
||
|
||
```bash
|
||
cd ai-toolkit #in case you are not yet in the ai-toolkit folder
|
||
huggingface-cli login #provide a `write` token to publish your LoRA at the end
|
||
python flux_train_ui.py
|
||
```
|
||
|
||
You will instantiate a UI that will let you upload your images, caption them, train and publish your LoRA
|
||

|
||
|
||
|
||
## Training in RunPod
|
||
Example RunPod template: **runpod/pytorch:2.2.0-py3.10-cuda12.1.1-devel-ubuntu22.04**
|
||
> You need a minimum of 24GB VRAM, pick a GPU by your preference.
|
||
|
||
#### Example config ($0.5/hr):
|
||
- 1x A40 (48 GB VRAM)
|
||
- 19 vCPU 100 GB RAM
|
||
|
||
#### Custom overrides (you need some storage to clone FLUX.1, store datasets, store trained models and samples):
|
||
- ~120 GB Disk
|
||
- ~120 GB Pod Volume
|
||
- Start Jupyter Notebook
|
||
|
||
### 1. Setup
|
||
```
|
||
git clone https://github.com/ostris/ai-toolkit.git
|
||
cd ai-toolkit
|
||
git submodule update --init --recursive
|
||
python -m venv venv
|
||
source venv/bin/activate
|
||
pip install torch
|
||
pip install -r requirements.txt
|
||
pip install --upgrade accelerate transformers diffusers huggingface_hub #Optional, run it if you run into issues
|
||
```
|
||
### 2. Upload your dataset
|
||
- Create a new folder in the root, name it `dataset` or whatever you like.
|
||
- Drag and drop your .jpg, .jpeg, or .png images and .txt files inside the newly created dataset folder.
|
||
|
||
### 3. Login into Hugging Face with an Access Token
|
||
- Get a READ token from [here](https://huggingface.co/settings/tokens) and request access to Flux.1-dev model from [here](https://huggingface.co/black-forest-labs/FLUX.1-dev).
|
||
- Run ```huggingface-cli login``` and paste your token.
|
||
|
||
### 4. Training
|
||
- Copy an example config file located at ```config/examples``` to the config folder and rename it to ```whatever_you_want.yml```.
|
||
- Edit the config following the comments in the file.
|
||
- Change ```folder_path: "/path/to/images/folder"``` to your dataset path like ```folder_path: "/workspace/ai-toolkit/your-dataset"```.
|
||
- Run the file: ```python run.py config/whatever_you_want.yml```.
|
||
|
||
### Screenshot from RunPod
|
||
<img width="1728" alt="RunPod Training Screenshot" src="https://github.com/user-attachments/assets/53a1b8ef-92fa-4481-81a7-bde45a14a7b5">
|
||
|
||
## Training in Modal
|
||
|
||
### 1. Setup
|
||
#### ai-toolkit:
|
||
```
|
||
git clone https://github.com/ostris/ai-toolkit.git
|
||
cd ai-toolkit
|
||
git submodule update --init --recursive
|
||
python -m venv venv
|
||
source venv/bin/activate
|
||
pip install torch
|
||
pip install -r requirements.txt
|
||
pip install --upgrade accelerate transformers diffusers huggingface_hub #Optional, run it if you run into issues
|
||
```
|
||
#### Modal:
|
||
- Run `pip install modal` to install the modal Python package.
|
||
- Run `modal setup` to authenticate (if this doesn’t work, try `python -m modal setup`).
|
||
|
||
#### Hugging Face:
|
||
- Get a READ token from [here](https://huggingface.co/settings/tokens) and request access to Flux.1-dev model from [here](https://huggingface.co/black-forest-labs/FLUX.1-dev).
|
||
- Run `huggingface-cli login` and paste your token.
|
||
|
||
### 2. Upload your dataset
|
||
- Drag and drop your dataset folder containing the .jpg, .jpeg, or .png images and .txt files in `ai-toolkit`.
|
||
|
||
### 3. Configs
|
||
- Copy an example config file located at ```config/examples/modal``` to the `config` folder and rename it to ```whatever_you_want.yml```.
|
||
- Edit the config following the comments in the file, **<ins>be careful and follow the example `/root/ai-toolkit` paths</ins>**.
|
||
|
||
### 4. Edit run_modal.py
|
||
- Set your entire local `ai-toolkit` path at `code_mount = modal.Mount.from_local_dir` like:
|
||
|
||
```
|
||
code_mount = modal.Mount.from_local_dir("/Users/username/ai-toolkit", remote_path="/root/ai-toolkit")
|
||
```
|
||
- Choose a `GPU` and `Timeout` in `@app.function` _(default is A100 40GB and 2 hour timeout)_.
|
||
|
||
### 5. Training
|
||
- Run the config file in your terminal: `modal run run_modal.py --config-file-list-str=/root/ai-toolkit/config/whatever_you_want.yml`.
|
||
- You can monitor your training in your local terminal, or on [modal.com](https://modal.com/).
|
||
- Models, samples and optimizer will be stored in `Storage > flux-lora-models`.
|
||
|
||
### 6. Saving the model
|
||
- Check contents of the volume by running `modal volume ls flux-lora-models`.
|
||
- Download the content by running `modal volume get flux-lora-models your-model-name`.
|
||
- Example: `modal volume get flux-lora-models my_first_flux_lora_v1`.
|
||
|
||
### Screenshot from Modal
|
||
|
||
<img width="1728" alt="Modal Traning Screenshot" src="https://github.com/user-attachments/assets/7497eb38-0090-49d6-8ad9-9c8ea7b5388b">
|
||
|
||
---
|
||
|
||
## Dataset Preparation
|
||
|
||
Datasets generally need to be a folder containing images and associated text files. Currently, the only supported
|
||
formats are jpg, jpeg, and png. Webp currently has issues. The text files should be named the same as the images
|
||
but with a `.txt` extension. For example `image2.jpg` and `image2.txt`. The text file should contain only the caption.
|
||
You can add the word `[trigger]` in the caption file and if you have `trigger_word` in your config, it will be automatically
|
||
replaced.
|
||
|
||
Images are never upscaled but they are downscaled and placed in buckets for batching. **You do not need to crop/resize your images**.
|
||
The loader will automatically resize them and can handle varying aspect ratios.
|
||
|
||
|
||
## Training Specific Layers
|
||
|
||
To train specific layers with LoRA, you can use the `only_if_contains` network kwargs. For instance, if you want to train only the 2 layers
|
||
used by The Last Ben, [mentioned in this post](https://x.com/__TheBen/status/1829554120270987740), you can adjust your
|
||
network kwargs like so:
|
||
|
||
```yaml
|
||
network:
|
||
type: "lora"
|
||
linear: 128
|
||
linear_alpha: 128
|
||
network_kwargs:
|
||
only_if_contains:
|
||
- "transformer.single_transformer_blocks.7.proj_out"
|
||
- "transformer.single_transformer_blocks.20.proj_out"
|
||
```
|
||
|
||
The naming conventions of the layers are in diffusers format, so checking the state dict of a model will reveal
|
||
the suffix of the name of the layers you want to train. You can also use this method to only train specific groups of weights.
|
||
For instance to only train the `single_transformer` for FLUX.1, you can use the following:
|
||
|
||
```yaml
|
||
network:
|
||
type: "lora"
|
||
linear: 128
|
||
linear_alpha: 128
|
||
network_kwargs:
|
||
only_if_contains:
|
||
- "transformer.single_transformer_blocks."
|
||
```
|
||
|
||
You can also exclude layers by their names by using `ignore_if_contains` network kwarg. So to exclude all the single transformer blocks,
|
||
|
||
|
||
```yaml
|
||
network:
|
||
type: "lora"
|
||
linear: 128
|
||
linear_alpha: 128
|
||
network_kwargs:
|
||
ignore_if_contains:
|
||
- "transformer.single_transformer_blocks."
|
||
```
|
||
|
||
`ignore_if_contains` takes priority over `only_if_contains`. So if a weight is covered by both,
|
||
if will be ignored.
|
||
|
||
## LoKr Training
|
||
|
||
To learn more about LoKr, read more about it at [KohakuBlueleaf/LyCORIS](https://github.com/KohakuBlueleaf/LyCORIS/blob/main/docs/Guidelines.md). To train a LoKr model, you can adjust the network type in the config file like so:
|
||
|
||
```yaml
|
||
network:
|
||
type: "lokr"
|
||
lokr_full_rank: true
|
||
lokr_factor: 8
|
||
```
|
||
|
||
Everything else should work the same including layer targeting.
|
||
|