space: load the released nf4 Warden from the hub (1GB Space LFS cap rules out in-repo weights)
Browse files- space/app.py +27 -16
space/app.py
CHANGED
|
@@ -57,15 +57,20 @@ STATIC = Path(__file__).parent / "static"
|
|
| 57 |
|
| 58 |
# ------------------------------------------------------------ the Warden brain
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
#
|
| 62 |
-
# the
|
|
|
|
|
|
|
|
|
|
| 63 |
_SHIPPED = REPO_ROOT / "model"
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
)
|
|
|
|
|
|
|
| 69 |
INTERNAL_KEY = os.environ.get("SCRYPT_INTERNAL_KEY") or secrets.token_hex(16)
|
| 70 |
|
| 71 |
tok = None
|
|
@@ -128,21 +133,26 @@ if spaces is not None:
|
|
| 128 |
_ensure_mamba_kernels()
|
| 129 |
import torch
|
| 130 |
from transformers import (
|
|
|
|
| 131 |
AutoModelForCausalLM,
|
| 132 |
AutoTokenizer,
|
| 133 |
BitsAndBytesConfig,
|
| 134 |
)
|
| 135 |
|
| 136 |
-
tok = AutoTokenizer.from_pretrained(
|
|
|
|
|
|
|
| 137 |
# 4-bit fits the `large` (48GB) slice and matches the Q4 GGUF the
|
| 138 |
-
# local game ships — quality parity, not a downgrade. The
|
| 139 |
-
#
|
| 140 |
-
# only quantize on the fly when
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
| 143 |
_quant_kwargs = (
|
| 144 |
{}
|
| 145 |
-
if
|
| 146 |
else {
|
| 147 |
"quantization_config": BitsAndBytesConfig(
|
| 148 |
load_in_4bit=True,
|
|
@@ -153,6 +163,7 @@ if spaces is not None:
|
|
| 153 |
)
|
| 154 |
model = AutoModelForCausalLM.from_pretrained(
|
| 155 |
MODEL_ID,
|
|
|
|
| 156 |
trust_remote_code=True,
|
| 157 |
device_map="cuda",
|
| 158 |
**_quant_kwargs,
|
|
@@ -221,7 +232,7 @@ def status() -> dict:
|
|
| 221 |
"warden_ready": WARDEN_READY,
|
| 222 |
"warden_error": WARDEN_ERR,
|
| 223 |
"mamba": MAMBA_DIAG,
|
| 224 |
-
"model": MODEL_ID,
|
| 225 |
"zerogpu": spaces is not None,
|
| 226 |
}
|
| 227 |
|
|
|
|
| 57 |
|
| 58 |
# ------------------------------------------------------------ the Warden brain
|
| 59 |
|
| 60 |
+
# Model source, in preference order:
|
| 61 |
+
# 1. WARDEN_MODEL env (+ optional WARDEN_SUBFOLDER)
|
| 62 |
+
# 2. weights shipped in the repo at model/ (only possible with persistent
|
| 63 |
+
# storage — Space repos cap LFS at 1GB, so this dir is normally absent)
|
| 64 |
+
# 3. the released Warden, pre-quantized nf4 (~18GB, HF-internal download —
|
| 65 |
+
# this is what kills the boot-time 63GB download + quantize wait)
|
| 66 |
_SHIPPED = REPO_ROOT / "model"
|
| 67 |
+
if os.environ.get("WARDEN_MODEL"):
|
| 68 |
+
MODEL_ID = os.environ["WARDEN_MODEL"]
|
| 69 |
+
SUBFOLDER = os.environ.get("WARDEN_SUBFOLDER", "")
|
| 70 |
+
elif any(_SHIPPED.glob("*.safetensors")):
|
| 71 |
+
MODEL_ID, SUBFOLDER = str(_SHIPPED), ""
|
| 72 |
+
else:
|
| 73 |
+
MODEL_ID, SUBFOLDER = "IMJONEZZ/warden-nemotron-3-nano-30b", "bnb-4bit"
|
| 74 |
INTERNAL_KEY = os.environ.get("SCRYPT_INTERNAL_KEY") or secrets.token_hex(16)
|
| 75 |
|
| 76 |
tok = None
|
|
|
|
| 133 |
_ensure_mamba_kernels()
|
| 134 |
import torch
|
| 135 |
from transformers import (
|
| 136 |
+
AutoConfig,
|
| 137 |
AutoModelForCausalLM,
|
| 138 |
AutoTokenizer,
|
| 139 |
BitsAndBytesConfig,
|
| 140 |
)
|
| 141 |
|
| 142 |
+
tok = AutoTokenizer.from_pretrained(
|
| 143 |
+
MODEL_ID, subfolder=SUBFOLDER, trust_remote_code=True
|
| 144 |
+
)
|
| 145 |
# 4-bit fits the `large` (48GB) slice and matches the Q4 GGUF the
|
| 146 |
+
# local game ships — quality parity, not a downgrade. The released
|
| 147 |
+
# bnb-4bit weights are already nf4 (config carries
|
| 148 |
+
# quantization_config); only quantize on the fly when pointed at
|
| 149 |
+
# raw BF16 weights.
|
| 150 |
+
_cfg = AutoConfig.from_pretrained(
|
| 151 |
+
MODEL_ID, subfolder=SUBFOLDER, trust_remote_code=True
|
| 152 |
+
)
|
| 153 |
_quant_kwargs = (
|
| 154 |
{}
|
| 155 |
+
if getattr(_cfg, "quantization_config", None)
|
| 156 |
else {
|
| 157 |
"quantization_config": BitsAndBytesConfig(
|
| 158 |
load_in_4bit=True,
|
|
|
|
| 163 |
)
|
| 164 |
model = AutoModelForCausalLM.from_pretrained(
|
| 165 |
MODEL_ID,
|
| 166 |
+
subfolder=SUBFOLDER,
|
| 167 |
trust_remote_code=True,
|
| 168 |
device_map="cuda",
|
| 169 |
**_quant_kwargs,
|
|
|
|
| 232 |
"warden_ready": WARDEN_READY,
|
| 233 |
"warden_error": WARDEN_ERR,
|
| 234 |
"mamba": MAMBA_DIAG,
|
| 235 |
+
"model": MODEL_ID + (f"/{SUBFOLDER}" if SUBFOLDER else ""),
|
| 236 |
"zerogpu": spaces is not None,
|
| 237 |
}
|
| 238 |
|