Skip to content

Commit 83bbc30

Browse files
authored
Merge branch 'dev' into main
2 parents de3f397 + c84e02f commit 83bbc30

31 files changed

Lines changed: 1510 additions & 112 deletions

CHANGELOG.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,70 @@
11
# CHANGELOG
22

3+
* **NEXT MAIN**
4+
5+
* **Diffusers v0.9.0, Stable Diffusion v2.0**. Models:
6+
* `"stabilityai/stable-diffusion-2"` - trained on 768x768
7+
* `"stabilityai/stable-diffusion-2-base"` - trained on 512x512
8+
* `"stabilityai/stable-diffusion-2-inpainting"` - untested
9+
* `""stabilityai/stable-diffusion-x4-upscaler"` - untested
10+
11+
> https://github.com/huggingface/diffusers/releases
12+
13+
* **DPMSolverMultistepScheduler**. Docker-diffusers-api is simply a wrapper
14+
around diffusers. We support all the included schedulers out of the box,
15+
as long as they can init themselves with default arguments. So, the above
16+
scheduler was already working, but we didn't mention it before. I'll just
17+
quote diffusers:
18+
19+
> DPMSolverMultistepScheduler is the firecracker diffusers implementation
20+
of DPM-Solver++, a state-of-the-art scheduler that was contributed by one
21+
of the authors of the paper. This scheduler is able to achieve great
22+
quality in as few as 20 steps. It's a drop-in replacement for the default
23+
Stable Diffusion scheduler, so you can use it to essentially half
24+
generation times.
25+
26+
* **Storage Class / S3 support**. We now have a generic storage class, which
27+
allows for special URLs anywhere anywhere you can usually specify a URL,
28+
e.g. `CHECKPOINT_URL`, `dest_url` (after dreambooth training), and the new
29+
`MODEL_URL` (see below). URLs like "s3:///bucket/filename" will work how
30+
you expect, but definitely read [docs/storage.md](./docs/storage.md)
31+
to understand the format better. Note in particular the triple forwardslash
32+
("///") in the beginning to use the default S3 endpoint.
33+
34+
* **Dreambooth training**, working but still in development. See
35+
[this forum post](https://banana-forums.dev/t/dreambooth-training-first-look/36)
36+
for more info.
37+
38+
* **`PRECISION`** build var, defaults to `"fp16"`, set to `""` to use the model
39+
defaults (generally fp32).
40+
41+
* **`CHECKPOINT_URL` conversion**:
42+
* Crash / stop build if conversion fails (rather than unclear errors later on)
43+
* Force `cpu` loading even for models that would otherwise default to GPU.
44+
This fixes certain models that previously crashed in build stage (where GPU
45+
is not available).
46+
* `--extract-ema` on conversion since these are the more important weights for
47+
inference.
48+
* `CHECKPOINT_CONFIG_URL` now let's to specify a specific config file for
49+
conversion, to use instead of SD's default `v1-inference.yaml`.
50+
51+
* **`MODEL_URL`**. If your model is already in diffusers format, but you don't
52+
host it on HuggingFace, you can now have it downloaded at build time. At
53+
this stage, it should be a `.tar.zst` file. This is an *alternative* to
54+
`CHECKPOINT_URL` which downloads a `.ckpt` file and converts to diffusers.
55+
56+
* **`test.py`**:
57+
* New `--banana` arg to run the test on banana. Set environment variables
58+
`BANANA_API_KEY` and `BANANA_MODEL_KEY` first.
59+
* You can now add to and override a test's default json payload with:
60+
* `--model-arg prompt="hello"`
61+
* `--call-arg MODEL_ID="my-model"`
62+
* Support for extra timing data (e.g. dreambooth sends `train`
63+
and `upload` timings).
64+
65+
* **Dev: better caching solution**. No more unruly `root-cache` directory. See
66+
[CONTRIBUTING.md](./CONTRIBUTING.md) for more info.
67+
368
* **2022-11-08**
469

570
* **Much faster `init()` times!** For `runwayml/stable-diffusion-v1-5`:

CONTRIBUTING.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# CONTRIBUTING
2+
3+
*Tips for development*
4+
5+
1. [Using Buildkit](#buildkit)
6+
1. [Local HTTP(S) Caching Proxy](#caching)
7+
1. [Local S3 Server](#local-s3-server)
8+
9+
<a name="buildkit"></a>
10+
## Using BuildKit
11+
12+
Buildkit is a docker extension that can really improve build speeds through
13+
caching and parallelization. You can enable and tweak it by adding:
14+
15+
`DOCKER_BUILDKIT=1 BUILDKIT_PROGRESS=plain`
16+
17+
vars before `docker build` (the `PROGRESS` var shows much more detailed
18+
build logs, which can be useful, but are much more verbose). This is
19+
already all setup in the the [build](./build) script.
20+
21+
<a name="caching"></a>
22+
## Local HTTP(S) Caching Proxy
23+
24+
If you're only editing e.g. `app.py`, there's no need to worry about caching
25+
and the docker layers work amazingly. But, if you're constantly changing
26+
installed packages (apt, `requirements.txt`), `download.py`, etc, it's VERY
27+
helpful to have a local cache:
28+
29+
```bash
30+
# See all options at https://hub.docker.com/r/gadicc/squid-ssl-zero
31+
$ docker run -d -p 3128:3128 -p 3129:80 \
32+
--name squid --restart=always \
33+
-v /usr/local/squid:/usr/local/squid \
34+
gadicc/squid-ssl-zero
35+
```
36+
37+
and then set the docker build args `proxy=1`, and `http_proxy` / `https_proxy`
38+
with their respective values.
39+
This is already all set up in the [build](./build) script.
40+
41+
**You probably want to fine-tune /usr/local/squid/etc/squid.conf**.
42+
43+
It will be created after you first run `gadicc/squid-ssl-zero`. You can then
44+
stop the container (`docker ps`, `docker stop container_id`), edit the file,
45+
and re-start (`docker start container_id`). For now, try something like:
46+
47+
```conf
48+
cache_dir ufs /usr/local/squid/cache 50000 16 256 # 50GB
49+
maximum_object_size 20 GB
50+
refresh_pattern . 52034400 50% 52034400 store-stale override-expire ignore-no-cache ignore-no-store ignore-private
51+
```
52+
53+
but ideally we can as a community create some rules that don't so
54+
aggressively catch every single request.
55+
56+
<a name="local-s3"></a>
57+
## Local S3 server
58+
59+
If you're doing development around the S3 handling, it can be very useful to
60+
have a local S3 server, especially due to the large size of models. You
61+
can set one up like this:
62+
63+
```bash
64+
$ docker run -p 9000:9000 -p 9001:9001 \
65+
-v /usr/local/minio:/data quay.io/minio/minio \
66+
server /data --console-address ":9001"
67+
```
68+
69+
Now point a web browser to http://localhost:9001/, login with the default
70+
root credentials `minioadmin:minioadmin` and create a bucket and credentials
71+
for testing. More info at https://hub.docker.com/r/minio/minio/.
72+
73+
Typical policy:
74+
75+
```json
76+
{
77+
"Version": "2012-10-17",
78+
"Statement": [
79+
{
80+
"Sid": "VisualEditor0",
81+
"Effect": "Allow",
82+
"Action": [
83+
"s3:PutObject",
84+
"s3:GetObject"
85+
],
86+
"Resource": "arn:aws:s3:::BUCKET_NAME/*"
87+
}
88+
]
89+
}
90+
```

Dockerfile

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@
77
FROM pytorch/pytorch:1.12.1-cuda11.3-cudnn8-runtime as base
88
# Below: our ideal image, but Optimization fails with it.
99
#FROM continuumio/miniconda3:4.12.0 as base
10+
11+
# Note, docker uses HTTP_PROXY and HTTPS_PROXY (uppercase)
12+
# We purposefully want those managed independently, as we want docker
13+
# to manage its own cache. This is just for pip, models, etc.
14+
ARG http_proxy
15+
ENV http_proxy=${http_proxy}
16+
ARG https_proxy
17+
ENV https_proxy=${https_proxy}
18+
RUN if [ -n "$http_proxy" ] ; then \
19+
echo quit \
20+
| openssl s_client -proxy $(echo ${https_proxy} | cut -b 8-) -servername google.com -connect google.com:443 -showcerts \
21+
| sed 'H;1h;$!d;x; s/^.*\(-----BEGIN CERTIFICATE-----.*-----END CERTIFICATE-----\)\n---\nServer certificate.*$/\1/' \
22+
> /usr/local/share/ca-certificates/squid-self-signed.crt ; \
23+
update-ca-certificates ; \
24+
fi
25+
ENV REQUESTS_CA_BUNDLE=${http_proxy:+/usr/local/share/ca-certificates/squid-self-signed.crt}
26+
1027
ENV DEBIAN_FRONTEND=noninteractive
1128
#RUN apt-get install gnupg2
1229
#RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys A4B469963BF863CC
@@ -31,31 +48,35 @@ FROM base as output
3148
RUN mkdir /api
3249
WORKDIR /api
3350

51+
## XXXX playing around a lot.
52+
# pip installs pytorch 1.13 and uninstalls 1.12 (needed by xformers)
53+
# recomment conda update; didn't help. need to solve above issue.
54+
55+
RUN conda update -n base -c defaults conda
3456
# We need python 3.9 or 3.10 for xformers
3557
# Yes, we install pytorch twice... will switch base image in future
36-
# RUN conda update -n base -c defaults conda
3758
RUN conda create -n xformers python=3.10
3859
SHELL ["/opt/conda/bin/conda", "run", "--no-capture-output", "-n", "xformers", "/bin/bash", "-c"]
3960
RUN python --version
4061
RUN conda install -c pytorch -c conda-forge cudatoolkit=11.6 pytorch=1.12.1
4162
RUN conda install xformers -c xformers/label/dev
4263

4364
# Install python packages
44-
RUN mkdir -p /root/.cache/pip
45-
COPY root-cache/pip /root/.cache/pip
46-
RUN pip3 install --upgrade pip
65+
# RUN pip3 install --upgrade pip
66+
RUN https_proxy="" REQUESTS_CA_BUNDLE="" conda install pip
4767
ADD requirements.txt requirements.txt
48-
RUN pip3 install -r requirements.txt
68+
RUN pip install -r requirements.txt
4969

50-
# Required to build flash attention
70+
# Not needed anymore, but, may be needed again in the future :D
5171
# Turing: 7.5 (RTX 20s, Quadro), Ampere: 8.0 (A100), 8.6 (RTX 30s)
5272
# https://arnon.dk/matching-sm-architectures-arch-and-gencode-for-various-nvidia-cards/
53-
# ENV FLASH_ATTENTION=0
5473
# ENV TORCH_CUDA_ARCH_LIST="7.5 8.0 8.6"
55-
# this is built it into memory efficient attention now ! ^_^
5674

57-
ADD install.sh .
58-
RUN bash install.sh
75+
RUN git clone https://github.com/huggingface/diffusers
76+
WORKDIR /api/diffusers
77+
RUN git checkout v0.9.0
78+
WORKDIR /api
79+
RUN pip install -e diffusers
5980

6081
# We add the banana boilerplate here
6182
ADD server.py .
@@ -76,20 +97,44 @@ ENV HF_AUTH_TOKEN=${HF_AUTH_TOKEN}
7697
ARG MODEL_ID="runwayml/stable-diffusion-v1-5"
7798
ENV MODEL_ID=${MODEL_ID}
7899

100+
# "" = model default.
101+
ARG PRECISION="fp16"
102+
ENV PRECISION=${PRECISION}
103+
ADD precision.py .
104+
79105
# ARG PIPELINE="StableDiffusionInpaintPipeline"
80106
ARG PIPELINE="ALL"
81107
ENV PIPELINE=${PIPELINE}
82108

83-
COPY root-cache/huggingface /root/.cache/huggingface
84-
COPY root-cache/checkpoints /root/.cache/checkpoints
85-
RUN du -sh /root/.cache/*
109+
ARG USE_DREAMBOOTH=0
110+
ENV USE_DREAMBOOTH=${USE_DREAMBOOTH}
86111

112+
ARG AWS_ACCESS_KEY_ID
113+
ARG AWS_SECRET_ACCESS_KEY
114+
ARG AWS_DEFAULT_REGION
115+
ARG AWS_S3_ENDPOINT_URL
116+
ENV AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
117+
ENV AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
118+
ENV AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION}
119+
ENV AWS_S3_ENDPOINT_URL=${AWS_S3_ENDPOINT_URL}
120+
121+
COPY utils utils
122+
123+
# Download diffusers model from somewhere else (see Storage docs)
124+
# Don't use this for checkpoints (.ckpt)! Use CHECKPOINT_URL for that.
125+
ARG MODEL_URL=""
126+
ENV MODEL_URL=${MODEL_URL}
87127
# If set, it will be downloaded and converted to diffusers format, and
88128
# saved in a directory with same MODEL_ID name to be loaded by diffusers.
89129
ARG CHECKPOINT_URL=""
90130
ENV CHECKPOINT_URL=${CHECKPOINT_URL}
131+
ARG CHECKPOINT_CONFIG_URL=""
132+
ENV CHECKPOINT_CONFIG_URL=${CHECKPOINT_CONFIG_URL}
133+
91134
ADD download-checkpoint.py .
92135
RUN python3 download-checkpoint.py
136+
ARG _CONVERT_SPECIAL
137+
ENV _CONVERT_SPECIAL=${_CONVERT_SPECIAL}
93138
ADD convert-to-diffusers.py .
94139
RUN python3 convert-to-diffusers.py
95140
# RUN rm -rf checkpoints
@@ -106,7 +151,15 @@ ARG USE_PATCHMATCH=0
106151
RUN if [ "$USE_PATCHMATCH" = "1" ] ; then apt-get install -yqq python3-opencv ; fi
107152
COPY --from=patchmatch /tmp/PyPatchMatch PyPatchMatch
108153

154+
RUN if [ "$USE_DREAMBOOTH" = "1" ] ; then \
155+
# By specifying the same torch version as conda, it won't download again.
156+
# Without this, it will upgrade torch, break xformers, make bigger image.
157+
pip install -r diffusers/examples/dreambooth/requirements.txt bitsandbytes torch==1.12.1 ; \
158+
fi
159+
RUN if [ "$USE_DREAMBOOTH" = "1" ] ; then apt-get install git-lfs ; fi
160+
109161
# Add your custom app code, init() and inference()
162+
ADD train_dreambooth.py .
110163
ADD send.py .
111164
ADD app.py .
112165

README.md

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@ Note: This image was created for [kiri.art](https://kiri.art/).
1818
Everything is open source but there may be certain request / response
1919
assumptions. If anything is unclear, please open an issue.
2020

21-
## [Read the CHANGELOG for Important Updates.](./CHANGELOG.md)
21+
## Updates and Help
22+
23+
* [Official `docker-diffusers-api` Forum](https://banana-forums.dev/c/open-source/docker-diffusers-api/16):
24+
help, updates, discussion.
25+
* Subscribe ("watch") these forum topics for:
26+
* [notable **`main`** branch updates](https://banana-forums.dev/t/official-releases-main-branch/35)
27+
* [notable **`dev`** branch updates](https://banana-forums.dev/t/development-releases-dev-branch/53)
28+
* Always [check the CHANGELOG](./CHANGELOG.md) for important updates when upgrading.
2229

2330
**Official help in our dedicated forum https://banana-forums.dev/c/open-source/docker-diffusers-api/16.**
2431

@@ -48,11 +55,8 @@ serverless).
4855

4956
**Building**
5057

51-
1. Set `HF_AUTH_TOKEN` environment var if you haven't set it elsewhere.
5258
1. `docker build -t banana-sd --build-arg HF_AUTH_TOKEN=$HF_AUTH_TOKEN .`
53-
1. Optionally add `DOCKER_BUILDKIT=1 BUILDKIT_PROGRESS=plain` to
54-
start of the line, depending on your preferences. (Recommended if
55-
you're using the `root-cache` feature.)
59+
1. See [CONTRIBUTING.md](./CONTRIBUTING.md) for more helpful hints.
5660
1. Note: your first build can take a really long time, depending on
5761
your PC & network speed, and *especially when using the `CHECKPOINT_URL`
5862
feature*. Great time to grab a coffee or take a walk.
@@ -101,11 +105,14 @@ explicitly name `modelInputs` above, and send a bigger object (with
101105

102106
If provided, `init_image` and `mask_image` should be base64 encoded.
103107

104-
Available schedulers: `LMSDiscreteScheduler`, `DDIMScheduler`, `PNDMScheduler`,
105-
`EulerAncestralDiscreteScheduler`, `EulerDiscreteScheduler`. These cover the
106-
most commonly used / requested schedulers, but we already have code in place to
107-
support every scheduler provided by diffusers, which will work in a later
108-
diffusers release when they have better defaults.
108+
**Schedulers**: docker-diffusers-api is simply a wrapper around diffusers,
109+
literally any scheduler included in diffusers will work out of the box,
110+
provided it can loaded with its default config and without requiring
111+
any other explicit arguments at init time. In any event, the following
112+
schedulers are the most common and most well tested:
113+
`DPMSolverMultistepScheduler` (fast! only needs 20 steps!),
114+
`LMSDiscreteScheduler`, `DDIMScheduler`, `PNDMScheduler`,
115+
`EulerAncestralDiscreteScheduler`, `EulerDiscreteScheduler`.
109116

110117
<a name="testing"></a>
111118
## Examples and testing

0 commit comments

Comments
 (0)