From 767c8195ef0435ef3127b822cfef41ece1c407d0 Mon Sep 17 00:00:00 2001 From: drbh Date: Mon, 29 Apr 2024 16:36:53 +0000 Subject: [PATCH] hold: docker stuff --- .gitignore | 2 + .../mydockerinstaller.egg-info/PKG-INFO | 3 + .../mydockerinstaller.egg-info/SOURCES.txt | 8 + .../dependency_links.txt | 0 .../entry_points.txt | 2 + .../mydockerinstaller.egg-info/top_level.txt | 1 + dockerinstaller/mydockerinstaller/__init__.py | 1 + .../mydockerinstaller/installer.py | 231 ++++++++++++++++++ dockerinstaller/setup.py | 10 + 9 files changed, 258 insertions(+) create mode 100644 dockerinstaller/mydockerinstaller.egg-info/PKG-INFO create mode 100644 dockerinstaller/mydockerinstaller.egg-info/SOURCES.txt create mode 100644 dockerinstaller/mydockerinstaller.egg-info/dependency_links.txt create mode 100644 dockerinstaller/mydockerinstaller.egg-info/entry_points.txt create mode 100644 dockerinstaller/mydockerinstaller.egg-info/top_level.txt create mode 100644 dockerinstaller/mydockerinstaller/__init__.py create mode 100644 dockerinstaller/mydockerinstaller/installer.py create mode 100644 dockerinstaller/setup.py diff --git a/.gitignore b/.gitignore index b3ca772b..2ac2f6b4 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ server/exllama_kernels/exllama_kernels/hip_func/ *_hip.cuh server/exllama_kernels/exllama_kernels/hip_buffers.cuh server/exllama_kernels/exllama_kernels/exllama_ext_hip.cpp + +data/ diff --git a/dockerinstaller/mydockerinstaller.egg-info/PKG-INFO b/dockerinstaller/mydockerinstaller.egg-info/PKG-INFO new file mode 100644 index 00000000..2207a21e --- /dev/null +++ b/dockerinstaller/mydockerinstaller.egg-info/PKG-INFO @@ -0,0 +1,3 @@ +Metadata-Version: 2.1 +Name: mydockerinstaller +Version: 0.1 diff --git a/dockerinstaller/mydockerinstaller.egg-info/SOURCES.txt b/dockerinstaller/mydockerinstaller.egg-info/SOURCES.txt new file mode 100644 index 00000000..4d00dcb0 --- /dev/null +++ b/dockerinstaller/mydockerinstaller.egg-info/SOURCES.txt @@ -0,0 +1,8 @@ +setup.py +mydockerinstaller/__init__.py +mydockerinstaller/installer.py +mydockerinstaller.egg-info/PKG-INFO +mydockerinstaller.egg-info/SOURCES.txt +mydockerinstaller.egg-info/dependency_links.txt +mydockerinstaller.egg-info/entry_points.txt +mydockerinstaller.egg-info/top_level.txt diff --git a/dockerinstaller/mydockerinstaller.egg-info/dependency_links.txt b/dockerinstaller/mydockerinstaller.egg-info/dependency_links.txt new file mode 100644 index 00000000..e69de29b diff --git a/dockerinstaller/mydockerinstaller.egg-info/entry_points.txt b/dockerinstaller/mydockerinstaller.egg-info/entry_points.txt new file mode 100644 index 00000000..888ccafe --- /dev/null +++ b/dockerinstaller/mydockerinstaller.egg-info/entry_points.txt @@ -0,0 +1,2 @@ +[console_scripts] +mydockerinstaller = mydockerinstaller.installer:main diff --git a/dockerinstaller/mydockerinstaller.egg-info/top_level.txt b/dockerinstaller/mydockerinstaller.egg-info/top_level.txt new file mode 100644 index 00000000..473b721f --- /dev/null +++ b/dockerinstaller/mydockerinstaller.egg-info/top_level.txt @@ -0,0 +1 @@ +mydockerinstaller diff --git a/dockerinstaller/mydockerinstaller/__init__.py b/dockerinstaller/mydockerinstaller/__init__.py new file mode 100644 index 00000000..cd343d7c --- /dev/null +++ b/dockerinstaller/mydockerinstaller/__init__.py @@ -0,0 +1 @@ +from installer import run_docker_container, DockerThread diff --git a/dockerinstaller/mydockerinstaller/installer.py b/dockerinstaller/mydockerinstaller/installer.py new file mode 100644 index 00000000..654fd9f1 --- /dev/null +++ b/dockerinstaller/mydockerinstaller/installer.py @@ -0,0 +1,231 @@ +import subprocess +import os +import sys + + +def install_docker(): + try: + # Check if Docker is installed + subprocess.run(["docker", "--version"], check=True) + except subprocess.CalledProcessError: + # Determine the OS and install Docker + if os.name == "posix": + plat = sys.platform + if plat.startswith("linux"): + subprocess.run(["sudo", "apt-get", "update"], check=True) + subprocess.run( + ["sudo", "apt-get", "install", "-y", "docker.io"], check=True + ) + elif plat == "darwin": + subprocess.run(["brew", "install", "--cask", "docker"], check=True) + else: + sys.exit("Unsupported OS for Linux-like environments: {}".format(plat)) + elif os.name == "nt": + subprocess.run(["choco", "install", "docker-desktop"], check=True) + else: + sys.exit("Unsupported OS: {}".format(os.name)) + + +def install_nvidia_toolkit(): + if sys.platform.startswith("linux"): + subprocess.run(["sudo", "apt-get", "update"], check=True) + subprocess.run( + ["sudo", "apt-get", "install", "-y", "nvidia-container-toolkit"], check=True + ) + else: + print("NVIDIA Container Toolkit is not supported on this OS.") + + +def run_docker_container(model, volume, detach=False): + subprocess.run( + [ + "docker", + "run", + # conditionally add the -d flag to run the container in the background + # *(["-d"] if detach else []), + "-it", + "--gpus", + "all", + "--shm-size", + "1g", + "-p", + "8080:80", + "-v", + "{}:/data".format(volume), + "ghcr.io/huggingface/text-generation-inference:latest", + "--model-id", + model, + ], + check=True, + ) + + +def run_detached_docker_container(model, volume): + subprocess.run( + [ + "docker", + "run", + "-d", + "-it", + "--gpus", + "all", + "--shm-size", + "1g", + "-p", + "8080:80", + "-v", + "{}:/data".format(volume), + "ghcr.io/huggingface/text-generation-inference:latest", + "--model-id", + model, + ], + check=True, + ) + + +def main(): + print("Installing Docker...") + install_docker() + install_nvidia_toolkit() + run_docker_container("HuggingFaceH4/zephyr-7b-beta", os.getcwd() + "/data") + + +from threading import Thread +import threading + + +# a class that keeps the container running in a separate thread +class DockerThread(threading.Thread): + def __init__(self, model, volume): + threading.Thread.__init__(self) + self.model = model + self.volume = volume + + def run(self): + self._stop = threading.Event() + run_detached_docker_container(self.model, self.volume) + + def stop(self): + self._stop.set() + + +# how to use the DockerThread class +# import os + +# docker_thread = DockerThread("HuggingFaceH4/zephyr-7b-beta", os.getcwd() + "/data") + +# docker_thread.start() + +# do some other stuff + + +# import subprocess +# import os +# import sys + + +# def check_command_exists(command): +# """Check if a command exists in the system's PATH.""" +# return subprocess.run(["which", command], stdout=subprocess.PIPE).returncode == 0 + + +# def install_rust(): +# """Install Rust if it's not already installed.""" +# if not check_command_exists("cargo"): +# subprocess.run( +# ["curl", "--proto", "=https", "--tlsv1.2", "-sSf", "https://sh.rustup.rs"], +# stdout=subprocess.PIPE, +# ) +# subprocess.run(["sh", "rustup-init", "-y"], check=True) +# else: +# print("Rust is already installed.") + + +# def install_docker(): +# """Install Docker if it's not already installed.""" +# if check_command_exists("docker"): +# print("Docker is already installed.") +# return True +# try: +# if os.name == "posix": +# plat = sys.platform +# if plat.startswith("linux"): +# subprocess.run(["sudo", "apt-get", "update"], check=True) +# subprocess.run( +# ["sudo", "apt-get", "install", "-y", "docker.io"], check=True +# ) +# elif plat == "darwin": +# subprocess.run(["brew", "install", "--cask", "docker"], check=True) +# else: +# sys.exit(f"Unsupported OS for Linux-like environments: {plat}") +# elif os.name == "nt": +# subprocess.run(["choco", "install", "docker-desktop"], check=True) +# else: +# sys.exit(f"Unsupported OS: {os.name}") +# return True +# except subprocess.CalledProcessError: +# return False + + +# def install_nvidia_toolkit(): +# """Install NVIDIA toolkit if on Linux.""" +# if sys.platform.startswith("linux"): +# subprocess.run(["sudo", "apt-get", "update"], check=True) +# subprocess.run( +# ["sudo", "apt-get", "install", "-y", "nvidia-container-toolkit"], check=True +# ) +# else: +# print("NVIDIA Container Toolkit is not supported on this OS.") + + +# def clone_and_install(repo_url, directory): +# """Clone a repository and run 'make install'.""" +# os.chdir(directory) +# subprocess.run(["git", "clone", repo_url], check=True) +# os.chdir(repo_url.split("/")[-1]) +# subprocess.run(["make", "install"], check=True) + + +# def run_docker_container(model, volume): +# """Run a Docker container with specified parameters.""" +# subprocess.run( +# [ +# "docker", +# "run", +# "-it", +# "--gpus", +# "all", +# "--shm-size", +# "1g", +# "-p", +# "8080:80", +# "-v", +# f"{volume}:/data", +# "ghcr.io/huggingface/text-generation-inference:latest", +# "--model-id", +# model, +# ], +# check=True, +# ) + + +# def main(): +# repo_url = "https://github.com/example/repo.git" +# directory = "/path/to/clone" +# use_docker = input("Do you want to use Docker? (yes/no): ").strip().lower() == "yes" + +# install_rust() +# if use_docker: +# print("Installing Docker...") +# if install_docker(): +# install_nvidia_toolkit() +# run_docker_container("HuggingFaceH4/zephyr-7b-beta", os.getcwd() + "/data") +# else: +# print("Failed to install Docker. Attempting to build from source.") +# clone_and_install(repo_url, directory) +# else: +# clone_and_install(repo_url, directory) + + +# if __name__ == "__main__": +# main() diff --git a/dockerinstaller/setup.py b/dockerinstaller/setup.py new file mode 100644 index 00000000..63d5845a --- /dev/null +++ b/dockerinstaller/setup.py @@ -0,0 +1,10 @@ +from setuptools import setup, find_packages + +setup( + name="mydockerinstaller", + version="0.1", + packages=find_packages(), + entry_points={ + "console_scripts": ["mydockerinstaller = mydockerinstaller.installer:main"] + }, +)