Spaces:
Sleeping
Sleeping
Update app.py
Browse files12344567810910112 unmodified lines1415161920172218192021222328293031323324252627282930313 unmodified lines353637434445464748495051525354383940414243444546474849505152535455565758595859606160616263646566676566676869707172737475686970717273747576777879777880818281838485861 unmodified line88899089919192939495929394959697989997989910010210310410510610710810911011111210110210310410510612 unmodified lines1191201211311321331341351361371381391401411421221231241251471481491261271281291511301311322 unmodified lines1351361371601611381391401411421431671441451461471481731741751761491501511521532 unmodified lines1561571581851591601611622 unmodified lines1651661671941951961681691701711721991731741754 unmodified lines1801811822101831841851 unmodified line18718818921821922019019119219319419522619619719819920020120220323123220420520623420720820921021110 unmodified lines22222322425122522622722825522923023153 unmodified lines285286287315316317318319320288289290291292293294295296import osimport gradio as grimport torchimport numpy as npfrom transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamerfrom threading import Threadfrom concurrent.futures import ThreadPoolExecutorimport loggingimport timeimport queueimport threading
logging.basicConfig(level=logging.INFO)2 unmodified linesMODEL_ID = "YoussefElsafi/PlayerAI-1.2B-v1.5"
# ── CPU Threading setup FIRST (before any torch ops) ─────────────────────────# On CPU, torch uses OpenMP/MKL threads internally.# Too many = thrashing. Formula: physical_cores / expected_concurrent_usersPHYSICAL_CORES = os.cpu_count() or 2MAX_CONCURRENT = 2 # how many generations run truly in parallelMAX_CONCURRENT = 2THREADS_PER_GEN = max(1, PHYSICAL_CORES // MAX_CONCURRENT)
torch.set_num_threads(THREADS_PER_GEN)torch.set_num_interop_threads(2)
# Force MKL/OpenBLAS to respect our thread budget tooos.environ["OMP_NUM_THREADS"] = str(THREADS_PER_GEN)os.environ["MKL_NUM_THREADS"] = str(THREADS_PER_GEN)os.environ["OPENBLAS_NUM_THREADS"] = str(THREADS_PER_GEN)os.environ["VECLIB_MAXIMUM_THREADS"]= str(THREADS_PER_GEN)os.environ["NUMEXPR_NUM_THREADS"] = str(THREADS_PER_GEN)os.environ["OMP_NUM_THREADS"] = str(THREADS_PER_GEN)os.environ["MKL_NUM_THREADS"] = str(THREADS_PER_GEN)os.environ["OPENBLAS_NUM_THREADS"] = str(THREADS_PER_GEN)os.environ["VECLIB_MAXIMUM_THREADS"] = str(THREADS_PER_GEN)os.environ["NUMEXPR_NUM_THREADS"] = str(THREADS_PER_GEN)
logger.info(f"CPU cores: {PHYSICAL_CORES} | " f"Concurrent gens: {MAX_CONCURRENT} | "3 unmodified linesprint("Loading model...")tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
# On CPU: bfloat16 is FASTER than float32 on modern x86 (AVX-512 bf16 support)# Falls back gracefully if not supportedif torch.cuda.is_available(): DTYPE = torch.float16 DEVICE = "cuda"elif hasattr(torch, 'bfloat16') and torch.backends.cpu.is_avx512_bf16_supported(): DTYPE = torch.bfloat16 DEVICE = "cpu" logger.info("AVX-512 BF16 detected — using bfloat16")else: DTYPE = torch.float32 DEVICE = "cpu"# ── Safe dtype detection (works on ALL torch versions) ────────────────────────def _pick_dtype() -> torch.dtype: if torch.cuda.is_available(): logger.info("Device: CUDA — using float16") return torch.float16
# bfloat16 on CPU: supported natively on Ampere+ and some Intel CPUs. # We probe by actually trying a small bf16 op instead of relying on # version-specific API calls that may not exist. try: _ = torch.zeros(4, dtype=torch.bfloat16) @ torch.zeros(4, dtype=torch.bfloat16) logger.info("Device: CPU — bfloat16 probe passed, using bfloat16") return torch.bfloat16 except Exception: logger.info("Device: CPU — bfloat16 not supported, using float32") return torch.float32
DTYPE = _pick_dtype()DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
model = AutoModelForCausalLM.from_pretrained( MODEL_ID, torch_dtype = DTYPE, device_map = "cpu" if DEVICE == "cpu" else "auto", trust_remote_code = True, low_cpu_mem_usage = True, torch_dtype = DTYPE, device_map = "auto" if DEVICE == "cuda" else "cpu", trust_remote_code = True, low_cpu_mem_usage = True,)model.eval()
# ── INT8 dynamic quantization ─────────────────────────────────────────────────# ── INT8 dynamic quantization (biggest CPU speedup: ~2x faster, ~2x less RAM) ─# Works on Linear layers — the bulk of transformer computetry: model = torch.quantization.quantize_dynamic( model, {torch.nn.Linear}, dtype=torch.qint8, ) logger.info("INT8 dynamic quantization applied ✓")except Exception as e: logger.warning(f"Quantization skipped: {e}")# ~2× faster inference, ~50% less RAM. Only apply on CPU.if DEVICE == "cpu": try: model = torch.quantization.quantize_dynamic( model, {torch.nn.Linear}, dtype=torch.qint8, ) logger.info("INT8 dynamic quantization applied ✓") except Exception as e: logger.warning(f"Quantization skipped: {e}")
# ── torch.compile (PyTorch 2.0+) ──────────────────────────────────────────────# "default" mode works better than "reduce-overhead" on CPU# ── torch.compile ─────────────────────────────────────────────────────────────if hasattr(torch, "compile"): try: model = torch.compile(model, mode="default", backend="inductor") model = torch.compile(model, mode="default") logger.info("torch.compile applied ✓") except Exception as e: logger.warning(f"torch.compile skipped: {e}")1 unmodified lineprint("Model loaded!")
# ── Generation config ─────────────────────────────────────────────────────────# Shorter = faster. For chat, 80 tokens is plenty.GEN_CONFIG = dict( max_new_tokens = 80, # was 120 — shaves ~33% off latency do_sample = True, temperature = 0.85, top_p = 0.9, # repetition_penalty speeds up sampling by pruning logits max_new_tokens = 80, do_sample = True, temperature = 0.85, top_p = 0.9, repetition_penalty = 1.1, pad_token_id = tokenizer.eos_token_id, # Use cache — critical for autoregressive speed use_cache = True, pad_token_id = tokenizer.eos_token_id, use_cache = True,)
# ── Thread pool ───────────────────────────────────────────────────────────────executor = ThreadPoolExecutor( max_workers = MAX_CONCURRENT, thread_name_prefix = "gen",)
# ── Request queue + priority ───────────────────────────────────────────────────# Caps in-flight requests so we don't OOM with 16GB RAM# 1.2B model in int8 ≈ 1.2GB, each active KV cache ≈ 200MB# So 2 concurrent is safe, queue up to 8 waitingMAX_QUEUE = 8# ── Thread pool + semaphores ──────────────────────────────────────────────────executor = ThreadPoolExecutor(max_workers=MAX_CONCURRENT, thread_name_prefix="gen")MAX_QUEUE = 8gen_semaphore = threading.Semaphore(MAX_CONCURRENT)queue_counter = threading.Semaphore(MAX_QUEUE)
12 unmodified lines "Use lowercase, slang, and act like a real person chatting.")
# ── Prompt cache ───────────────────────────────────────────────────────────────# System prompt tokenized once and reused — saves ~5ms per request_SYSTEM_MESSAGES = [{"role": "system", "content": SYSTEM_PROMPT}]try: _SYSTEM_PROMPT_PREFIX = tokenizer.apply_chat_template( _SYSTEM_MESSAGES, tokenize=False, add_generation_prompt=False, )except Exception: _SYSTEM_PROMPT_PREFIX = ""
# ── Core generation ────────────────────────────────────────────────────────────def _run_generation(prompt: str, streamer: TextIteratorStreamer) -> None: inputs = tokenizer( prompt, return_tensors = "pt", truncation = True, max_length = 512, # hard cap — prevents runaway long contexts return_tensors = "pt", truncation = True, max_length = 512, ) # No .to(device) needed — model is already on cpu with torch.inference_mode(): model.generate( input_ids = inputs.input_ids,2 unmodified lines **GEN_CONFIG, )
# ── Streaming response ────────────────────────────────────────────────────────# ── Streaming response ─────────────────────────────────────────────────────────def respond(message: str, history: list, api_key: str): if not validate_key(api_key): yield "[error] unauthorized" return
# Queue guard — reject if backlog is full if not queue_counter.acquire(blocking=False): yield "[error] server busy — try again in a moment" return
try: # Build prompt — keep history short to limit context length # Long contexts = slow prefill on CPU MAX_HISTORY = 4 # last 4 turns only trimmed_history = history[-MAX_HISTORY:] if len(history) > MAX_HISTORY else history MAX_HISTORY = 4 trimmed_history = history[-MAX_HISTORY:]
messages = [{"role": "system", "content": SYSTEM_PROMPT}] messages.extend(trimmed_history)2 unmodified lines try: prompt = tokenizer.apply_chat_template( messages, tokenize = False, tokenize = False, add_generation_prompt = True, ) except Exception as e:2 unmodified lines
streamer = TextIteratorStreamer( tokenizer, skip_prompt = True, skip_special_tokens= True, timeout = 60.0, skip_prompt = True, skip_special_tokens = True, timeout = 60.0, )
# Acquire generation slot (blocks if MAX_CONCURRENT already running) gen_semaphore.acquire() try: future = executor.submit(_run_generation, prompt, streamer)4 unmodified lines yield response
future.result(timeout=10)
finally: gen_semaphore.release()
1 unmodified line queue_counter.release()
# ── Warm-up ────────────────────────────────────────────────────────────────────# Run one dummy generation at startup so the first real user isn't slow.# torch.compile needs a first pass to JIT-compile the graph.# ── Warmup ────────────────────────────────────────────────────────────────────def _warmup(): logger.info("Running warmup generation...") t0 = time.time() try: dummy_streamer = TextIteratorStreamer( tokenizer, skip_prompt=True, skip_special_tokens=True, timeout=120.0 tokenizer, skip_prompt = True, skip_special_tokens = True, timeout = 120.0, ) dummy_prompt = tokenizer.apply_chat_template( [{"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": "hi"}], tokenize=False, add_generation_prompt=True, tokenize = False, add_generation_prompt = True, ) inputs = tokenizer(dummy_prompt, return_tensors="pt", truncation=True, max_length=256) inputs = tokenizer(dummy_prompt, return_tensors="pt", truncation=True, max_length=256) warmup_cfg = {**GEN_CONFIG, "max_new_tokens": 20}
def _gen():10 unmodified lines for _ in dummy_streamer: pass t.join() logger.info(f"Warmup done in {time.time()-t0:.2f}s ✓") logger.info(f"Warmup done in {time.time() - t0:.2f}s ✓") except Exception as e: logger.warning(f"Warmup failed (non-fatal): {e}")
# Run warmup in background so server starts immediatelyThread(target=_warmup, daemon=True).start()
53 unmodified lines stored_key = gr.Textbox(value="", visible=False)
chatbot = gr.Chatbot( type = "messages", avatar_images = (USER_AVATAR, BOT_AVATAR), height = 500, show_label = False, bubble_full_width= False, show_copy_button = False, type = "messages", avatar_images = (USER_AVATAR, BOT_AVATAR), height = 500, show_label = False, bubble_full_width = False, show_copy_button = False, )
gr.ChatInterface(app.py+78-105