| """ |
| Sparse Model Loader β STREAMING MODE for Kimi K2.6 |
| |
| This version loads Kimi K2.6 DIRECTLY from HuggingFace's servers using mmap. |
| The 120GB model NEVER touches disk β only the relevant weight pages (4KB chunks) |
| get streamed into RAM on-demand. This is TRUE sparse loading: |
| |
| 120GB model on HF servers β only 2-8GB of relevant weights in RAM |
| |
| How it works: |
| 1. User asks a question |
| 2. SemanticRouter determines which layers/experts are needed |
| 3. llama-cpp-python requests those specific weight pages from HF Hub |
| 4. HF Hub streams only those pages into RAM (via HTTP range requests) |
| 5. Inference runs on the loaded pages |
| 6. Unused pages get evicted from RAM (OS manages this via mmap) |
| |
| Result: Run a 120GB model on 16GB RAM β 87% RAM savings! |
| """ |
|
|
| import os |
| import sys |
| import time |
| import json |
| import argparse |
| from typing import Optional, Generator |
|
|
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) |
|
|
| from semantic_router import SemanticRouter, RouteResult |
| from memory_monitor import MemoryMonitor |
| from fastapi import FastAPI, HTTPException |
| from fastapi.responses import StreamingResponse |
| from pydantic import BaseModel |
| import uvicorn |
|
|
| |
| KIMI_REPO = "unsloth/Kimi-K2.6-GGUF" |
| KIMI_FILE = "Kimi-K2.6-UD-IQ1_S.gguf" |
|
|
| app = FastAPI(title="Sparse Model Loader API β Kimi K2.6 Streaming") |
| router = SemanticRouter() |
| llm = None |
| model_loading = False |
|
|
|
|
| class ChatRequest(BaseModel): |
| model: str = "kimi-2.6" |
| messages: list |
| max_tokens: int = 4096 |
| temperature: float = 0.8 |
| stream: bool = False |
|
|
|
|
| def load_model_streaming(): |
| """Load Kimi K2.6 directly from HuggingFace Hub via mmap streaming.""" |
| global llm, model_loading |
|
|
| if llm is not None or model_loading: |
| return |
|
|
| model_loading = True |
|
|
| try: |
| from llama_cpp import Llama |
|
|
| |
| hf_token = os.environ.get("HF_TOKEN", "") |
|
|
| print(f"\nπ§ Loading Kimi K2.6 via STREAMING mmap from HuggingFace...") |
| print(f" Repo: {KIMI_REPO}") |
| print(f" File: {KIMI_FILE}") |
| print(f" Model stays on HF servers β only relevant params load into RAM") |
| print(f" This may take 30-60s for initial page loading...") |
|
|
| |
| |
| llm = Llama.from_pretrained( |
| repo_id=KIMI_REPO, |
| filename=KIMI_FILE, |
| n_ctx=4096, |
| n_threads=4, |
| n_gpu_layers=0, |
| use_mmap=True, |
| use_mlock=False, |
| n_batch=256, |
| verbose=False, |
| token=hf_token if hf_token else None, |
| ) |
|
|
| print(f"β
Kimi K2.6 loaded via streaming mmap!") |
| print(f" Only relevant weight pages are in RAM (2-8GB)") |
| print(f" Full 120GB model stays on HuggingFace servers") |
|
|
| except Exception as e: |
| print(f"β Failed to load Kimi K2.6: {e}") |
| print(f" Trying with hf_hub_download + local mmap...") |
|
|
| |
| try: |
| from huggingface_hub import hf_hub_download |
|
|
| hf_token = os.environ.get("HF_TOKEN", "") |
| os.makedirs("/data/models", exist_ok=True) |
|
|
| print(f" Downloading Kimi K2.6 to /data/models/...") |
| model_path = hf_hub_download( |
| repo_id=KIMI_REPO, |
| filename=KIMI_FILE, |
| local_dir="/data/models", |
| token=hf_token if hf_token else None, |
| resume_download=True, |
| ) |
| print(f" Downloaded: {model_path}") |
|
|
| from llama_cpp import Llama |
| llm = Llama( |
| model_path=model_path, |
| n_ctx=4096, |
| n_threads=4, |
| n_gpu_layers=0, |
| use_mmap=True, |
| use_mlock=False, |
| n_batch=256, |
| verbose=False, |
| ) |
| print(f"β
Kimi K2.6 loaded from local file with mmap!") |
|
|
| except Exception as e2: |
| print(f"β Fallback also failed: {e2}") |
| model_loading = False |
| raise |
|
|
| model_loading = False |
|
|
|
|
| @app.get("/v1/models") |
| async def models(): |
| return { |
| "object": "list", |
| "data": [{"id": "kimi-2.6", "object": "model", "owned_by": "sparse-loader"}], |
| } |
|
|
|
|
| @app.get("/status") |
| async def status(): |
| import psutil |
| return { |
| "status": "ok", |
| "model": "Kimi K2.6 (1T MoE)", |
| "model_loaded": llm is not None, |
| "model_loading": model_loading, |
| "ram_usage": f"{psutil.virtual_memory().percent}%", |
| "available_ram_gb": f"{psutil.virtual_memory().available / (1024**3):.1f}GB", |
| "mode": "streaming-mmap", |
| "description": "Kimi K2.6 loaded via streaming mmap β only relevant params in RAM", |
| } |
|
|
|
|
| @app.post("/v1/chat/completions") |
| async def chat_completions(req: ChatRequest): |
| |
| user_msg = "" |
| for msg in req.messages: |
| if msg["role"] == "user": |
| user_msg = msg["content"] |
|
|
| if not user_msg: |
| raise HTTPException(400, "No user message") |
|
|
| |
| route = router.route(user_msg) |
| print(f"\nπ Route: {route.expert} ({route.confidence:.0%}) β shards: {route.shard_ids}") |
| print(f" Reason: {route.reason}") |
|
|
| |
| if llm is None: |
| load_model_streaming() |
|
|
| print(f"β‘ Running inference with Kimi K2.6...") |
|
|
| if req.stream: |
| def generate(): |
| for chunk in llm.create_chat_completion( |
| messages=req.messages, |
| max_tokens=req.max_tokens, |
| temperature=req.temperature, |
| stream=True, |
| ): |
| delta = chunk["choices"][0].get("delta", {}).get("content", "") |
| if delta: |
| yield f"data: {json.dumps({'choices': [{'delta': {'content': delta}}]})}\n\n" |
| yield "data: [DONE]\n\n" |
|
|
| return StreamingResponse(generate(), media_type="text/event-stream") |
| else: |
| response = llm.create_chat_completion( |
| messages=req.messages, |
| max_tokens=req.max_tokens, |
| temperature=req.temperature, |
| ) |
| return response |
|
|
|
|
| if __name__ == "__main__": |
| parser = argparse.ArgumentParser(description="Sparse Model Loader β Kimi K2.6 Streaming") |
| parser.add_argument("--serve", action="store_true", help="Start API server") |
| parser.add_argument("--port", type=int, default=7860, help="API port") |
| parser.add_argument("--ram", type=float, default=16.0, help="Max RAM in GB") |
| args = parser.parse_args() |
|
|
| if args.serve: |
| print(f"\nπ API Server starting on port {args.port}") |
| print(f" Model: Kimi K2.6 (1T MoE) via streaming mmap") |
| print(f" Max RAM: {args.ram}GB") |
| print(f" OpenAI-compatible: http://0.0.0.0:{args.port}/v1/chat/completions") |
| uvicorn.run(app, host="0.0.0.0", port=args.port) |
|
|