Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use uv in backend container #8605

Merged
merged 47 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
d486230
[syft] use uv in backend
yashgorana Mar 20, 2024
21d83cb
Merge branch 'dev' into yash/backend-uv
yashgorana Mar 20, 2024
5253f18
[syft] support for arm64 builds
yashgorana Mar 21, 2024
e7348cc
[syft] add pip and upgrade uv
yashgorana Mar 26, 2024
9f123cd
Merge branch 'dev' into yash/backend-uv
yashgorana Mar 29, 2024
bbac713
[syft] fixes + use dockerfile.dockerignore
yashgorana Mar 29, 2024
6653edb
Merge branch 'dev' into yash/backend-uv
yashgorana Apr 3, 2024
1de78de
[syft] update torch uv
yashgorana Apr 8, 2024
c51c025
[syft] fix worker_cpu.dockerfile
yashgorana Apr 8, 2024
6d16bb6
[syft] fix worker_cpu.dockerfile
yashgorana Apr 8, 2024
14a4ec3
Merge branch 'dev' into yash/backend-uv
yashgorana Apr 8, 2024
93ca8f2
[syft] fix gitignore
yashgorana Apr 8, 2024
242599f
Merge branch 'dev' into yash/backend-uv
yashgorana Apr 8, 2024
94acf1d
[syft] uv http timeout
yashgorana Apr 8, 2024
848864c
[syft] remove oblv
yashgorana Apr 8, 2024
de27469
Merge branch 'dev' into yash/backend-uv
yashgorana Apr 8, 2024
823f3ec
Merge branch 'dev' into yash/backend-uv
yashgorana Apr 10, 2024
53ed988
[veilid] remove dockerignore
yashgorana Apr 10, 2024
b2bb657
Merge branch 'dev' into yash/backend-uv
yashgorana Apr 10, 2024
b8ee669
[syft] fix worker not starting
yashgorana Apr 10, 2024
3a3573d
Merge branch 'dev' into yash/backend-uv
yashgorana Apr 11, 2024
a85db75
Merge branch 'dev' into yash/backend-uv
yashgorana Apr 11, 2024
8b63cac
Merge branch 'dev' into yash/backend-uv
yashgorana Apr 11, 2024
126c138
Merge branch 'dev' into yash/backend-uv
yashgorana Apr 17, 2024
b9b57cc
[syft] another way to fix torch cpu
yashgorana Apr 17, 2024
6fb53ab
Merge branch 'dev' into yash/backend-uv
yashgorana Apr 20, 2024
10be86d
Merge branch 'dev' into yash/backend-uv
yashgorana Apr 22, 2024
b566213
Merge branch 'dev' into yash/backend-uv
yashgorana Apr 23, 2024
93d1c15
Merge branch 'dev' into yash/backend-uv
yashgorana Apr 23, 2024
00ea37a
Merge branch 'dev' into yash/backend-uv
yashgorana Apr 25, 2024
9bfd4c3
Merge branch 'dev' into yash/backend-uv
yashgorana Apr 29, 2024
03ad6fc
Merge branch 'dev' into yash/backend-uv
yashgorana Apr 29, 2024
04346c8
Merge branch 'dev' into yash/backend-uv
yashgorana May 7, 2024
e702be9
[syft] wait for seaweedfs to be ready
yashgorana May 7, 2024
755a15e
[k8s] make registry a bit more reliable on docker macos
yashgorana May 7, 2024
3a21269
[syft] bump backend torch & uv
yashgorana May 7, 2024
1464d05
[k8s] remove k3d registry volume
yashgorana May 7, 2024
4a62333
[k8s] tweak destroy command
yashgorana May 7, 2024
dd1e8bd
update uv
yashgorana May 8, 2024
b2e1f88
Merge branch 'dev' into yash/backend-uv
yashgorana May 8, 2024
cfd5092
Merge branch 'dev' into yash/backend-uv
yashgorana May 8, 2024
914b2a2
Merge branch 'dev' into yash/backend-uv
yashgorana May 14, 2024
9032c4c
fixes
yashgorana May 14, 2024
ce24621
drop telemetry
yashgorana May 14, 2024
822031a
Merge branch 'dev' into yash/backend-uv
yashgorana May 15, 2024
e34f6c7
fix dockerignore paths
yashgorana May 15, 2024
c7f7598
Merge branch 'dev' into yash/backend-uv
yashgorana May 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 0 additions & 11 deletions .dockerignore

This file was deleted.

11 changes: 0 additions & 11 deletions packages/.dockerignore

This file was deleted.

138 changes: 52 additions & 86 deletions packages/grid/backend/backend.dockerfile
Original file line number Diff line number Diff line change
@@ -1,94 +1,70 @@
ARG PYTHON_VERSION="3.12"
ARG TZ="Etc/UTC"

# change to USER="syftuser", UID=1000 and HOME="/home/$USER" for rootless
ARG USER="root"
ARG UID=0
ARG USER_GRP=$USER:$USER
ARG HOME="/root"
ARG APPDIR="$HOME/app"
ARG UV_VERSION="0.1.41-r0"
ARG TORCH_VERSION="2.3.0"

# ==================== [BUILD STEP] Python Dev Base ==================== #

FROM cgr.dev/chainguard/wolfi-base as python_dev
FROM cgr.dev/chainguard/wolfi-base as syft_deps

ARG PYTHON_VERSION
ARG TZ
ARG USER
ARG UID
ARG UV_VERSION
ARG TORCH_VERSION

# Setup Python DEV
RUN --mount=type=cache,target=/var/cache/apk,sharing=locked \
apk update && \
apk upgrade && \
apk add build-base gcc tzdata python-$PYTHON_VERSION-dev-default py$PYTHON_VERSION-pip && \
ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# uncomment for creating rootless user
# && adduser -D -u $UID $USER

# ==================== [BUILD STEP] Install Syft Dependency ==================== #

FROM python_dev as syft_deps

ARG APPDIR
ARG HOME
ARG UID
ARG USER
ARG USER_GRP

USER $USER
WORKDIR $APPDIR
ENV PATH=$PATH:$HOME/.local/bin

# copy skeleton to do package install
COPY --chown=$USER_GRP \
syft/setup.py \
syft/setup.cfg \
syft/pyproject.toml \
syft/MANIFEST.in \
syft/

COPY --chown=$USER_GRP \
syft/src/syft/VERSION \
syft/src/syft/capnp \
syft/src/syft/

# Install all dependencies together here to avoid any version conflicts across pkgs
RUN --mount=type=cache,id=pip-$UID,target=$HOME/.cache/pip,uid=$UID,gid=$UID,sharing=locked \
pip install --user --default-timeout=300 torch==2.2.1 -f https://download.pytorch.org/whl/cpu/torch_stable.html && \
pip install --user pip-autoremove jupyterlab -e ./syft[data_science] && \
pip-autoremove ansible ansible-core -y
RUN apk update && apk upgrade && \
apk add build-base gcc python-$PYTHON_VERSION-dev-default uv=$UV_VERSION

WORKDIR /root/app

ENV UV_HTTP_TIMEOUT=600

# keep static deps separate to have each layer cached independently
# if amd64 then we need to append +cpu to the torch version
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish PyTorch would fix this somehow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that --index-url for cpu installs is annoying because setup.cfg doesn't support it - else things would've been easier.

Plus uv also has this issue with +cpu monikers at the moment. I've mentioned it here - astral-sh/uv#1497 (comment) & they're looking into it astral-sh/uv#3437.

# uv issues: https://github.com/astral-sh/uv/issues/3437 & https://github.com/astral-sh/uv/issues/2541
RUN --mount=type=cache,target=/root/.cache,sharing=locked \
uv venv && \
ARCH=$(arch | sed s/aarch64/arm64/ | sed s/x86_64/amd64/) && \
if [[ "$ARCH" = "amd64" ]]; then TORCH_VERSION="$TORCH_VERSION+cpu"; fi && \
uv pip install torch==$TORCH_VERSION --index-url https://download.pytorch.org/whl/cpu

COPY syft/setup.py syft/setup.cfg syft/pyproject.toml ./syft/

COPY syft/src/syft/VERSION ./syft/src/syft/

RUN --mount=type=cache,target=/root/.cache,sharing=locked \
# remove torch because we already have the cpu version pre-installed
sed --in-place /torch==/d ./syft/setup.cfg && \
uv pip install -e ./syft[data_science] && \
uv pip freeze | grep ansible | xargs uv pip uninstall

# ==================== [Final] Setup Syft Server ==================== #

FROM cgr.dev/chainguard/wolfi-base as backend

# inherit from global
ARG APPDIR
ARG HOME
ARG PYTHON_VERSION
ARG TZ
ARG USER
ARG USER_GRP

# Setup Python
RUN --mount=type=cache,target=/var/cache/apk,sharing=locked \
apk update && \
apk upgrade && \
apk add tzdata git bash python-$PYTHON_VERSION-default py$PYTHON_VERSION-pip && \
ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && \
# Uncomment for rootless user
# adduser -D -u 1000 $USER && \
mkdir -p /var/log/pygrid $HOME/data/creds $HOME/data/db $HOME/.cache $HOME/.local
# chown -R $USER_GRP /var/log/pygrid $HOME/

USER $USER
WORKDIR $APPDIR
ARG UV_VERSION

RUN apk update && apk upgrade && \
apk add --no-cache git bash python-$PYTHON_VERSION-default py$PYTHON_VERSION-pip uv=$UV_VERSION

WORKDIR /root/app/

# Copy pre-built syft dependencies
COPY --from=syft_deps /root/app/.venv .venv

# copy grid
COPY grid/backend/grid ./grid/

# copy syft
COPY syft ./syft/

# Update environment variables
ENV PATH=$PATH:$HOME/.local/bin \
PYTHONPATH=$APPDIR \
APPDIR=$APPDIR \
ENV \
# "activate" venv
PATH="/root/app/.venv/bin/:$PATH" \
VIRTUAL_ENV="/root/app/.venv" \
# Syft
APPDIR="/root/app" \
NODE_NAME="default_node_name" \
NODE_TYPE="domain" \
SERVICE_NAME="backend" \
Expand All @@ -102,16 +78,6 @@ ENV PATH=$PATH:$HOME/.local/bin \
MONGO_HOST="localhost" \
MONGO_PORT="27017" \
MONGO_USERNAME="root" \
MONGO_PASSWORD="example" \
CREDENTIALS_PATH="$HOME/data/creds/credentials.json"

# Copy pre-built jupyterlab, syft dependencies
COPY --chown=$USER_GRP --from=syft_deps $HOME/.local $HOME/.local

# copy grid
COPY --chown=$USER_GRP grid/backend/grid ./grid/

# copy syft
COPY --chown=$USER_GRP syft/ ./syft/
MONGO_PASSWORD="example"

CMD ["bash", "./grid/start.sh"]
63 changes: 63 additions & 0 deletions packages/grid/backend/backend.dockerfile.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Paths should be against the docker root context dir i.e. /packages

# Syft
**/tests/
**/*.md

# Byte-compiled / optimized / DLL files
**/__pycache__/
**/*.py[cod]
**/*$py.class

# Distribution / packaging
**/.Python
**/build/
**/develop-eggs/
**/dist/
**/downloads/
**/eggs/
**/.eggs/
**/lib/
**/lib64/
**/parts/
**/sdist/
**/var/
**/wheels/
**/share/python-wheels/
**/*.egg-info/
**/.installed.cfg
**/*.egg
**/MANIFEST

# Jupyter Notebook
**/.ipynb_checkpoints

# Environments
**/.env
**/.venv
**/env/
**/venv/
**/ENV/
**/env.bak/
**/venv.bak/

# Unit test / coverage reports
**/htmlcov/
**/.tox/
**/.nox/
**/.coverage
**/.coverage.*
**/.cache
**/nosetests.xml
**/coverage.xml
**/*.cover
**/*.py,cover
**/.hypothesis/
**/.pytest_cache/
**/cover/

# vim
**/*.swp

# macOS
**/.DS_Store
3 changes: 1 addition & 2 deletions packages/grid/backend/grid/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ def get_env(key: str, default: str = "") -> str | None:
return None


DEFAULT_CREDENTIALS_PATH = os.path.expandvars("$HOME/data/creds/credentials.json")
CREDENTIALS_PATH = str(get_env("CREDENTIALS_PATH", DEFAULT_CREDENTIALS_PATH))
CREDENTIALS_PATH = str(get_env("CREDENTIALS_PATH", "credentials.json"))
NODE_PRIVATE_KEY = "NODE_PRIVATE_KEY"
NODE_UID = "NODE_UID"

Expand Down
15 changes: 7 additions & 8 deletions packages/grid/backend/grid/images/worker_cpu.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ ARG PIP_PACKAGES="pip --dry-run"
ARG CUSTOM_CMD='echo "No custom commands passed"'

# Worker specific environment variables go here
ENV SYFT_WORKER="true"
ENV SYFT_VERSION_TAG=${SYFT_VERSION_TAG}
ENV SYFT_WORKER="true" \
SYFT_VERSION_TAG=${SYFT_VERSION_TAG} \
UV_HTTP_TIMEOUT=600

# Commenting this until we support built using python docker sdk or find any other alternative.
# RUN --mount=type=cache,target=/var/cache/apk,sharing=locked \
# --mount=type=cache,target=$HOME/.cache/pip,sharing=locked \
RUN apk update && \
apk add ${SYSTEM_PACKAGES} && \
pip install --user ${PIP_PACKAGES} && \
RUN apk update && apk upgrade && \
apk add --no-cache ${SYSTEM_PACKAGES} && \
# if uv is present then run uv pip install else simple pip install
if [ -x "$(command -v uv)" ]; then uv pip install --no-cache ${PIP_PACKAGES}; else pip install --user ${PIP_PACKAGES}; fi && \
bash -c "$CUSTOM_CMD"
15 changes: 4 additions & 11 deletions packages/grid/backend/grid/start.sh
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
#! /usr/bin/env bash
set -e

echo "Running start.sh with RELEASE=${RELEASE} and $(id)"
export GEVENT_MONKEYPATCH="False"
echo "Running Syft with RELEASE=${RELEASE} and $(id)"

APP_MODULE=grid.main:app
LOG_LEVEL=${LOG_LEVEL:-info}
HOST=${HOST:-0.0.0.0}
PORT=${PORT:-80}
NODE_TYPE=${NODE_TYPE:-domain}
APPDIR=${APPDIR:-$HOME/app}

RELOAD=""
DEBUG_CMD=""

# For debugging permissions
ls -lisa $HOME/data
ls -lisa $APPDIR/syft/
ls -lisa $APPDIR/grid/

if [[ ${DEV_MODE} == "True" ]];
then
echo "DEV_MODE Enabled"
yashgorana marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -28,15 +21,15 @@ fi
# only set by kubernetes to avoid conflict with docker tests
if [[ ${DEBUGGER_ENABLED} == "True" ]];
then
pip install --user debugpy
uv pip install debugpy
DEBUG_CMD="python -m debugpy --listen 0.0.0.0:5678 -m"
fi

set +e
export CREDENTIALS_PATH=${CREDENTIALS_PATH:-$HOME/data/creds/credentials.json}
export NODE_PRIVATE_KEY=$(python $APPDIR/grid/bootstrap.py --private_key)
export NODE_UID=$(python $APPDIR/grid/bootstrap.py --uid)
export NODE_TYPE=$NODE_TYPE
set -e
export GEVENT_MONKEYPATCH="False"

echo "NODE_UID=$NODE_UID"
echo "NODE_TYPE=$NODE_TYPE"
Expand Down
4 changes: 0 additions & 4 deletions packages/grid/frontend/.dockerignore

This file was deleted.

15 changes: 15 additions & 0 deletions packages/grid/frontend/frontend.dockerfile.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Paths should be relative to the context dir of this image i.e. /packages/grid/frontend/

# Frontend
rasswanth-s marked this conversation as resolved.
Show resolved Hide resolved
**/*.md

# Dependency directories
**/node_modules
**/.svelte-kit
**/.pnpm-store

# vim
**/*.swp

# macOS
**/.DS_Store