Elffuss BraidHub

kikocis/llama-stack public

Make the "all-remote" distribution lightweight in dependencies and size
Changeset6527984cb4642900648fdd6b3e7829e62315c7bb3a1188dbfba84d2c9e93b0d1
AuthorAshwin Bharambe <ashwin.bharambe@gmail.com> · 2024-09-24 14:18 -07:00
CommitterAshwin Bharambe <ashwin.bharambe@gmail.com> · 2024-09-24 14:18 -07:00
Parents148d094ca6bd

Browse the files at this changeset

4 files changed

modified llama_stack/distribution/build.py +14 −3
@@ -66,6 +66,14 @@
6666 if provider_spec.docker_image:
6767 raise ValueError("A stack's dependencies cannot have a docker image")
6868
69+ special_deps = []
70+ deps = []
71+ for package in package_deps.pip_packages:
72+ if "--no-deps" in package or "--index-url" in package:
73+ special_deps.append(package)
74+ else:
75+ deps.append(package)
76+
6977 if build_config.image_type == ImageType.docker.value:
7078 script = pkg_resources.resource_filename(
7179 "llama_stack", "distribution/build_container.sh"
@@ -75,7 +83,7 @@
7583 build_config.name,
7684 package_deps.docker_image,
7785 str(build_file_path),
78- " ".join(package_deps.pip_packages),
86+ " ".join(deps),
7987 ]
8088 else:
8189 script = pkg_resources.resource_filename(
@@ -84,14 +92,17 @@
8492 args = [
8593 script,
8694 build_config.name,
87- " ".join(package_deps.pip_packages),
95+ " ".join(deps),
8896 ]
8997
98+ if special_deps:
99+ args.append("#".join(special_deps))
100+
90101 return_code = run_with_pty(args)
91102 if return_code != 0:
92103 cprint(
93104 f"Failed to build target {build_config.name} with return code {return_code}",
94105 color="red",
95106 )
96-
107+
97108 return return_code
modified llama_stack/distribution/build_conda_env.sh +22 −9
@@ -17,14 +17,16 @@
1717 echo "Using llama-models-dir=$LLAMA_MODELS_DIR"
1818 fi
1919
20-set -euo pipefail
21-
22-if [ "$#" -ne 2 ]; then
23- echo "Usage: $0 <distribution_type> <build_name> <pip_dependencies>" >&2
20+if [ "$#" -lt 2 ]; then
21+ echo "Usage: $0 <distribution_type> <build_name> <pip_dependencies> [<special_pip_deps>]" >&2
2422 echo "Example: $0 <distribution_type> mybuild 'numpy pandas scipy'" >&2
2523 exit 1
2624 fi
2725
26+special_pip_deps="$3"
27+
28+set -euo pipefail
29+
2830 build_name="$1"
2931 env_name="llamastack-$build_name"
3032 pip_dependencies="$2"
@@ -43,6 +45,7 @@
4345 ensure_conda_env_python310() {
4446 local env_name="$1"
4547 local pip_dependencies="$2"
48+ local special_pip_deps="$3"
4649 local python_version="3.10"
4750
4851 # Check if conda command is available
@@ -78,7 +81,12 @@
7881 if [ -n "$TEST_PYPI_VERSION" ]; then
7982 # these packages are damaged in test-pypi, so install them first
8083 $CONDA_PREFIX/bin/pip install fastapi libcst
81- $CONDA_PREFIX/bin/pip install --extra-index-url https://test.pypi.org/simple/ llama-models==$TEST_PYPI_VERSION llama-stack==$TEST_PYPI_VERSION $pip_dependencies
84+ $CONDA_PREFIX/bin/pip install --extra-index-url https://test.pypi.org/simple/ \
85+ llama-models==$TEST_PYPI_VERSION llama-stack==$TEST_PYPI_VERSION \
86+ $pip_dependencies
87+ if [ -n "$special_pip_deps" ]; then
88+ $CONDA_PREFIX/bin/pip install --no-deps "$special_pip_deps"
89+ fi
8290 else
8391 # Re-installing llama-stack in the new conda environment
8492 if [ -n "$LLAMA_STACK_DIR" ]; then
@@ -105,11 +113,16 @@
105113 fi
106114
107115 # Install pip dependencies
108- if [ -n "$pip_dependencies" ]; then
109- printf "Installing pip dependencies: $pip_dependencies\n"
110- $CONDA_PREFIX/bin/pip install $pip_dependencies
116+ printf "Installing pip dependencies\n"
117+ $CONDA_PREFIX/bin/pip install $pip_dependencies
118+ if [ -n "$special_pip_deps" ]; then
119+ IFS='#' read -ra parts <<< "$special_pip_deps"
120+ for part in "${parts[@]}"; do
121+ echo "$part"
122+ $CONDA_PREFIX/bin/pip install $part
123+ done
111124 fi
112125 fi
113126 }
114127
115-ensure_conda_env_python310 "$env_name" "$pip_dependencies"
128+ensure_conda_env_python310 "$env_name" "$pip_dependencies" "$special_pip_deps"
modified llama_stack/distribution/build_container.sh +14 −5
@@ -4,12 +4,16 @@
44 LLAMA_STACK_DIR=${LLAMA_STACK_DIR:-}
55 TEST_PYPI_VERSION=${TEST_PYPI_VERSION:-}
66
7-if [ "$#" -ne 4 ]; then
8- echo "Usage: $0 <build_name> <docker_base> <pip_dependencies>
9- echo "Example: $0 my-fastapi-app python:3.9-slim 'fastapi uvicorn'
7+if [ "$#" -lt 4 ]; then
8+ echo "Usage: $0 <build_name> <docker_base> <pip_dependencies> [<special_pip_deps>]" >&2
9+ echo "Example: $0 my-fastapi-app python:3.9-slim 'fastapi uvicorn' " >&2
1010 exit 1
1111 fi
1212
13+special_pip_deps="$5"
14+
15+set -euo pipefail
16+
1317 build_name="$1"
1418 image_name="llamastack-$build_name"
1519 docker_base=$2
@@ -21,8 +25,6 @@
2125 GREEN='\033[0;32m'
2226 NC='\033[0m' # No Color
2327
24-set -euo pipefail
25-
2628 SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
2729 REPO_DIR=$(dirname $(dirname "$SCRIPT_DIR"))
2830 DOCKER_BINARY=${DOCKER_BINARY:-docker}
@@ -85,6 +87,13 @@
8587 add_to_docker "RUN pip install $pip_dependencies"
8688 fi
8789
90+if [ -n "$special_pip_deps" ]; then
91+ IFS='#' read -ra parts <<< "$special_pip_deps"
92+ for part in "${parts[@]}"; do
93+ add_to_docker "RUN pip install $part"
94+ done
95+fi
96+
8897 add_to_docker <<EOF
8998
9099 # This would be good in production but for debugging flexibility lets not add it right now
modified llama_stack/providers/registry/memory.py +15 −1
@@ -8,11 +8,25 @@
88
99 from llama_stack.distribution.datatypes import * # noqa: F403
1010
11+
1112 EMBEDDING_DEPS = [
1213 "blobfile",
1314 "chardet",
1415 "pypdf",
15- "sentence-transformers",
16+ "tqdm",
17+ "numpy",
18+ "scikit-learn",
19+ "scipy",
20+ "nltk",
21+ "sentencepiece",
22+ "transformers",
23+ # this happens to work because special dependencies are always installed last
24+ # so if there was a regular torch installed first, this would be ignored
25+ # we need a better way to do this to identify potential conflicts, etc.
26+ # for now, this lets us significantly reduce the size of the container which
27+ # does not have any "local" inference code (and hence does not need GPU-enabled torch)
28+ "torch --index-url https://download.pytorch.org/whl/cpu",
29+ "sentence-transformers --no-deps",
1630 ]
1731
1832