Download serve.py from gump2049/APUS-Reflex2.1: direct link, hf CLI and curl.
- Browser
- Download file 4.3 kB
-
https://huggingface.co/gump2049/APUS-Reflex2.1/resolve/main/serve.py
- Command line
-
hf download hf://gump2049/APUS-Reflex2.1/serve.py
-
curl -L -o serve.py https://huggingface.co/gump2049/APUS-Reflex2.1/resolve/main/serve.py
4.3 kB
| """Serve APUS-Reflex2.1 using an isolated runtime view of a read-only checkpoint.""" | |
| import argparse | |
| import json | |
| import os | |
| from pathlib import Path | |
| import signal | |
| import subprocess | |
| import sys | |
| import sysconfig | |
| import tempfile | |
| MODEL_ID = "APUS-Reflex2.1" | |
| PACKAGE = Path(__file__).resolve().parent | |
| INPUT_FILES = ( | |
| "config.json", | |
| "model.safetensors.index.json", | |
| "readout.safetensors", | |
| "tokenizer.json", | |
| "tokenizer_config.json", | |
| "vocab.json", | |
| "merges.txt", | |
| "chat_template.jinja", | |
| "preprocessor_config.json", | |
| "video_preprocessor_config.json", | |
| ) | |
| def build_view(model, view): | |
| index = json.loads((model / "model.safetensors.index.json").read_text()) | |
| shards = sorted(set(index["weight_map"].values())) | |
| if len(shards) != 11 or any(Path(name).name != name for name in shards): | |
| raise ValueError("Unexpected model shard layout") | |
| for name in (*INPUT_FILES, *shards): | |
| source = model / name | |
| if not source.is_file(): | |
| raise FileNotFoundError(source) | |
| (view / name).symlink_to(source.resolve()) | |
| config = json.loads((model / "decision_config.json").read_text()) | |
| if (config["format_id"], config["prompt"], config["attention_mode"]) != ( | |
| "apus-code-readout-v1", | |
| "apus", | |
| "prefix_bidir", | |
| ): | |
| raise ValueError("Unexpected decision configuration") | |
| (view / "decision_config.json").symlink_to( | |
| (model / "decision_config.json").resolve() | |
| ) | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument( | |
| "--model-dir", type=Path, default=Path(os.environ.get("MODEL_DIR", PACKAGE)) | |
| ) | |
| parser.add_argument("--host", default=os.environ.get("HOST", "127.0.0.1")) | |
| parser.add_argument("--port", type=int, default=os.environ.get("PORT", 8000)) | |
| parser.add_argument( | |
| "--tensor-parallel-size", | |
| type=int, | |
| default=os.environ.get("TENSOR_PARALLEL_SIZE", 2), | |
| ) | |
| parser.add_argument( | |
| "--gpu-memory-utilization", | |
| type=float, | |
| default=os.environ.get("GPU_MEMORY_UTILIZATION", "0.85"), | |
| ) | |
| args = parser.parse_args() | |
| model = args.model_dir.expanduser().resolve(strict=True) | |
| if not 1 <= args.port <= 65535 or args.tensor_parallel_size < 1: | |
| parser.error("Invalid port or tensor parallel size") | |
| if not 0 < args.gpu_memory_utilization <= 1: | |
| parser.error("GPU memory utilization must be greater than zero and at most one") | |
| subprocess.run( | |
| [sys.executable, PACKAGE / "third_party/vllm/install_backport.py"], check=True | |
| ) | |
| # On Hopper the configured GDN prefill dispatch imports its CuTe kernels lazily. | |
| # Check the same module before opening the API listener. | |
| try: | |
| from flashinfer.gdn_kernels.delta_rule_dsl import varlen_helper # noqa: F401 | |
| except ImportError as error: | |
| raise SystemExit( | |
| "The GDN prefill kernel is unavailable. Install requirements.txt in the serving environment." | |
| ) from error | |
| with tempfile.TemporaryDirectory(prefix="APUS-Reflex2.1-") as tmp: | |
| build_view(model, Path(tmp)) | |
| executable = Path(sysconfig.get_path("scripts")) / "vllm" | |
| cmd = [ | |
| executable, | |
| "serve", | |
| tmp, | |
| "--host", | |
| args.host, | |
| "--port", | |
| str(args.port), | |
| "--served-model-name", | |
| MODEL_ID, | |
| "--runner", | |
| "pooling", | |
| "--tensor-parallel-size", | |
| str(args.tensor_parallel_size), | |
| "--attention-backend", | |
| "TRITON_ATTN", | |
| "--disable-custom-all-reduce", | |
| "--enforce-eager", | |
| "--max-model-len", | |
| "131072", | |
| "--max-num-batched-tokens", | |
| "131072", | |
| "--max-num-seqs", | |
| "8", | |
| "--gpu-memory-utilization", | |
| str(args.gpu_memory_utilization), | |
| ] | |
| env = os.environ.copy() | |
| env.setdefault("VLLM_ALLREDUCE_USE_FLASHINFER", "0") | |
| process = subprocess.Popen(cmd, env=env) | |
| for signum in (signal.SIGTERM, signal.SIGINT): | |
| signal.signal(signum, lambda sig, frame: process.send_signal(sig)) | |
| return process.wait() | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |