From ae0e2914cdbfc56721463b77093d81e369386bf5 Mon Sep 17 00:00:00 2001 From: Shay Harding <3178267+kellewic@users.noreply.github.com> Date: Sat, 15 Mar 2025 19:34:14 +0000 Subject: [PATCH 1/2] Removed modal.build() in favor of using modal.Volume to cache model files. Added warming code as an app.function() --- week8/pricer_service2.py | 58 +++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/week8/pricer_service2.py b/week8/pricer_service2.py index 16d276b..25cf269 100644 --- a/week8/pricer_service2.py +++ b/week8/pricer_service2.py @@ -1,14 +1,18 @@ import modal -from modal import App, Volume, Image +from pathlib import PurePosixPath # Setup - define our infrastructure with code! - app = modal.App("pricer-service") -image = Image.debian_slim().pip_install("huggingface", "torch", "transformers", "bitsandbytes", "accelerate", "peft") -secrets = [modal.Secret.from_name("hf-secret")] -# Constants +image = modal.Image.debian_slim().pip_install( + "huggingface", "torch", "transformers", "bitsandbytes", + "accelerate", "peft", "huggingface_hub[hf_transfer]" +).env({"HF_HUB_ENABLE_HF_TRANSFER": "1"}) + +hf_cache_vol = modal.Volume.from_name("hf-cache", create_if_missing=True) +secrets = [modal.Secret.from_name("huggingface-secret")] +# Constants GPU = "T4" BASE_MODEL = "meta-llama/Meta-Llama-3.1-8B" PROJECT_NAME = "pricer" @@ -17,30 +21,28 @@ RUN_NAME = "2024-09-13_13.04.39" PROJECT_RUN_NAME = f"{PROJECT_NAME}-{RUN_NAME}" REVISION = "e8d637df551603dc86cd7a1598a8f44af4d7ae36" FINETUNED_MODEL = f"{HF_USER}/{PROJECT_RUN_NAME}" -MODEL_DIR = "hf-cache/" -BASE_DIR = MODEL_DIR + BASE_MODEL -FINETUNED_DIR = MODEL_DIR + FINETUNED_MODEL + +# Define cache locations - this will map to the volume created +MODEL_DIR = PurePosixPath("/models") +BASE_DIR = MODEL_DIR / BASE_MODEL +FINETUNED_DIR = MODEL_DIR / FINETUNED_MODEL QUESTION = "How much does this cost to the nearest dollar?" PREFIX = "Price is $" -@app.cls(image=image, secrets=secrets, gpu=GPU, timeout=1800) +@app.cls(image=image, secrets=secrets, gpu=GPU, timeout=1800, volumes={MODEL_DIR: hf_cache_vol}) class Pricer: - @modal.build() - def download_model_to_folder(self): - from huggingface_hub import snapshot_download - import os - os.makedirs(MODEL_DIR, exist_ok=True) - snapshot_download(BASE_MODEL, local_dir=BASE_DIR) - snapshot_download(FINETUNED_MODEL, revision=REVISION, local_dir=FINETUNED_DIR) - @modal.enter() def setup(self): - import os import torch - from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, set_seed + from huggingface_hub import snapshot_download + from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig from peft import PeftModel - + + # Download and cache model files to the volume + snapshot_download(BASE_MODEL, local_dir=BASE_DIR) + snapshot_download(FINETUNED_MODEL, revision=REVISION, local_dir=FINETUNED_DIR) + # Quant Config quant_config = BitsAndBytesConfig( load_in_4bit=True, @@ -48,9 +50,8 @@ class Pricer: bnb_4bit_compute_dtype=torch.bfloat16, bnb_4bit_quant_type="nf4" ) - + # Load model and tokenizer - self.tokenizer = AutoTokenizer.from_pretrained(BASE_DIR) self.tokenizer.pad_token = self.tokenizer.eos_token self.tokenizer.padding_side = "right" @@ -65,11 +66,8 @@ class Pricer: @modal.method() def price(self, description: str) -> float: - import os - import re - import torch - from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, set_seed - from peft import PeftModel + import re, torch + from transformers import set_seed set_seed(42) prompt = f"{QUESTION}\n\n{description}\n\n{PREFIX}" @@ -87,3 +85,9 @@ class Pricer: def wake_up(self) -> str: return "ok" +## Keep Pricer warm so it's faster to respond to requests +@app.function(schedule=modal.Period(seconds=50)) +def pricer_wake_up() -> str: + Pricer = modal.Cls.from_name("pricer-service", "Pricer") + return Pricer().wake_up.remote() + From 79948e3b71657ed4714c966d7dc20a5fed6f7992 Mon Sep 17 00:00:00 2001 From: Shay Harding <3178267+kellewic@users.noreply.github.com> Date: Sat, 22 Mar 2025 12:02:13 +0000 Subject: [PATCH 2/2] Removed wake code and added min_containers with a default of zero so no containers are kept active by default --- week8/pricer_service2.py | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/week8/pricer_service2.py b/week8/pricer_service2.py index 25cf269..031df1c 100644 --- a/week8/pricer_service2.py +++ b/week8/pricer_service2.py @@ -3,17 +3,20 @@ from pathlib import PurePosixPath # Setup - define our infrastructure with code! app = modal.App("pricer-service") +secrets = [modal.Secret.from_name("huggingface-secret")] image = modal.Image.debian_slim().pip_install( "huggingface", "torch", "transformers", "bitsandbytes", "accelerate", "peft", "huggingface_hub[hf_transfer]" ).env({"HF_HUB_ENABLE_HF_TRANSFER": "1"}) +# This is where we cache model files to avoid redownloading each time a container is started hf_cache_vol = modal.Volume.from_name("hf-cache", create_if_missing=True) -secrets = [modal.Secret.from_name("huggingface-secret")] -# Constants GPU = "T4" +# Keep N containers active to avoid cold starts +MIN_CONTAINERS = 0 + BASE_MODEL = "meta-llama/Meta-Llama-3.1-8B" PROJECT_NAME = "pricer" HF_USER = "ed-donner" # your HF name here! Or use mine if you just want to reproduce my results. @@ -22,7 +25,7 @@ PROJECT_RUN_NAME = f"{PROJECT_NAME}-{RUN_NAME}" REVISION = "e8d637df551603dc86cd7a1598a8f44af4d7ae36" FINETUNED_MODEL = f"{HF_USER}/{PROJECT_RUN_NAME}" -# Define cache locations - this will map to the volume created +# Mount for cache location MODEL_DIR = PurePosixPath("/models") BASE_DIR = MODEL_DIR / BASE_MODEL FINETUNED_DIR = MODEL_DIR / FINETUNED_MODEL @@ -30,7 +33,7 @@ FINETUNED_DIR = MODEL_DIR / FINETUNED_MODEL QUESTION = "How much does this cost to the nearest dollar?" PREFIX = "Price is $" -@app.cls(image=image, secrets=secrets, gpu=GPU, timeout=1800, volumes={MODEL_DIR: hf_cache_vol}) +@app.cls(image=image, secrets=secrets, gpu=GPU, timeout=1800, min_containers=MIN_CONTAINERS, volumes={MODEL_DIR: hf_cache_vol}) class Pricer: @modal.enter() def setup(self): @@ -81,13 +84,3 @@ class Pricer: match = re.search(r"[-+]?\d*\.\d+|\d+", contents) return float(match.group()) if match else 0 - @modal.method() - def wake_up(self) -> str: - return "ok" - -## Keep Pricer warm so it's faster to respond to requests -@app.function(schedule=modal.Period(seconds=50)) -def pricer_wake_up() -> str: - Pricer = modal.Cls.from_name("pricer-service", "Pricer") - return Pricer().wake_up.remote() -