-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDockerfile
More file actions
154 lines (120 loc) · 3.76 KB
/
Dockerfile
File metadata and controls
154 lines (120 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# syntax=docker/dockerfile:1
ARG BASE_IMAGE=nvidia/cuda:11.8.0-cudnn8-runtime-ubuntu22.04
ARG PYTHON_VERSION=3.10
ARG UV_VERSION=0.9
# Download uv binary stage
FROM "ghcr.io/astral-sh/uv:${UV_VERSION}" AS uv-reference
# Build uv and Python base stage
FROM "${BASE_IMAGE}" AS uv-python-base
ARG DEBIAN_FRONTEND=noninteractive
SHELL ["/bin/bash", "-euo", "pipefail", "-c"]
ENV PYTHONUNBUFFERED=1
ARG UV_VERSION
COPY --from=uv-reference /uv /uvx /bin/
ENV UV_PYTHON_CACHE_DIR="/uv_python_cache"
ENV UV_PYTHON_INSTALL_DIR="/opt/python"
ENV PATH="${UV_PYTHON_INSTALL_DIR}/bin:${PATH}"
ARG PYTHON_VERSION
RUN --mount=type=cache,target=/uv_python_cache <<EOF
uv python install "${PYTHON_VERSION}"
EOF
# Download sd-scripts source code stage
FROM "${BASE_IMAGE}" AS download-sd-scripts
ARG DEBIAN_FRONTEND=noninteractive
SHELL ["/bin/bash", "-euo", "pipefail", "-c"]
RUN --mount=type=cache,sharing=private,target=/var/cache/apt \
--mount=type=cache,sharing=private,target=/var/lib/apt/lists <<EOF
apt-get update
apt-get install -y --no-install-recommends \
git \
ca-certificates
EOF
ARG SD_SCRIPTS_URL=https://github.com/kohya-ss/sd-scripts
ARG SD_SCRIPTS_VERSION=206adb643848ff27894f1e72b6987fa66db99378
RUN <<EOF
mkdir -p /opt/sd-scripts
cd /opt/sd-scripts
git init
git remote add origin "${SD_SCRIPTS_URL}"
git fetch --depth 1 origin "${SD_SCRIPTS_VERSION}"
git checkout FETCH_HEAD
git submodule update --init --recursive
EOF
# Build Python virtual environment stage
FROM uv-python-base AS build-venv
# uv configuration
# - Generate bytecodes
# - Copy packages into virtual environment
ENV UV_COMPILE_BYTECODE=1
ENV UV_LINK_MODE=copy
# Install Python dependencies
COPY ./pyproject.toml uv.lock /opt/sd-scripts-venv-build/
RUN --mount=type=cache,target=/root/.cache/uv <<EOF
cd /opt/sd-scripts-venv-build/
UV_PROJECT_ENVIRONMENT="/opt/python_venv" uv sync --frozen --no-dev --no-editable --no-install-project
EOF
# Runtime stage
FROM uv-python-base AS runtime
RUN --mount=type=cache,sharing=private,target=/var/cache/apt \
--mount=type=cache,sharing=private,target=/var/lib/apt/lists <<EOF
apt-get update
apt-get install -y --no-install-recommends \
tk \
libglib2.0-0 \
libgl1-mesa-glx
EOF
# libnvrtc.so workaround
# https://github.com/aoirint/sd-scripts-docker/issues/19
RUN <<EOF
ln -s \
/usr/local/cuda-11.8/targets/x86_64-linux/lib/libnvrtc.so.11.2 \
/usr/local/cuda-11.8/targets/x86_64-linux/lib/libnvrtc.so
EOF
# Copy Python virtual environment from build stage
COPY --from=build-venv /opt/python_venv /opt/python_venv
ENV PATH="/opt/python_venv/bin:${PATH}"
# Copy application code
COPY --from=download-sd-scripts /opt/sd-scripts /opt/sd-scripts
# Install project and Pre-compile Python bytecode
RUN <<EOF
cd /opt/sd-scripts
UV_PROJECT_ENVIRONMENT="/opt/python_venv" uv pip install --editable .
python -m compileall .
EOF
# huggingface cache dir
ENV HF_HOME=/huggingface
RUN <<EOF
# create huggingface cache dir
mkdir -p /huggingface
# create accelerate cache dir
mkdir -p /huggingface/accelerate
tee /huggingface/accelerate/default_config.yaml <<EOT
command_file: null
commands: null
compute_environment: LOCAL_MACHINE
deepspeed_config: {}
distributed_type: 'NO'
downcast_bf16: 'no'
dynamo_backend: 'NO'
fsdp_config: {}
gpu_ids: all
machine_rank: 0
main_process_ip: null
main_process_port: null
main_training_function: main
megatron_lm_config: {}
mixed_precision: fp16
num_machines: 1
num_processes: 1
rdzv_backend: static
same_network: true
tpu_name: null
tpu_zone: null
use_cpu: false
EOT
# writable by default execution user
chown -R "1000:1000" /huggingface
EOF
WORKDIR /opt/sd-scripts
USER "1000:1000"
ENTRYPOINT [ "accelerate", "launch" ]