IMJONEZZ commited on
Commit
a6fd68b
·
1 Parent(s): b5186d6

space: load the Warden shipped in the repo (no boot download)

Browse files
Files changed (2) hide show
  1. .gitignore +3 -0
  2. space/app.py +26 -7
.gitignore CHANGED
@@ -28,3 +28,6 @@ finetune/_nemo_src/
28
  # OS / editor
29
  .DS_Store
30
  *.swp
 
 
 
 
28
  # OS / editor
29
  .DS_Store
30
  *.swp
31
+
32
+ # the Space ships its model via hf upload, never via local git
33
+ model/
space/app.py CHANGED
@@ -57,7 +57,15 @@ STATIC = Path(__file__).parent / "static"
57
 
58
  # ------------------------------------------------------------ the Warden brain
59
 
60
- MODEL_ID = os.environ.get("WARDEN_MODEL", "nvidia/NVIDIA-Nemotron-3-Nano-30B-A3B-BF16")
 
 
 
 
 
 
 
 
61
  INTERNAL_KEY = os.environ.get("SCRYPT_INTERNAL_KEY") or secrets.token_hex(16)
62
 
63
  tok = None
@@ -127,16 +135,27 @@ if spaces is not None:
127
 
128
  tok = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
129
  # 4-bit fits the `large` (48GB) slice and matches the Q4 GGUF the
130
- # local game ships — quality parity, not a downgrade.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
  model = AutoModelForCausalLM.from_pretrained(
132
  MODEL_ID,
133
  trust_remote_code=True,
134
- quantization_config=BitsAndBytesConfig(
135
- load_in_4bit=True,
136
- bnb_4bit_quant_type="nf4",
137
- bnb_4bit_compute_dtype=torch.bfloat16,
138
- ),
139
  device_map="cuda",
 
140
  )
141
  WARDEN_ERR = ""
142
  except Exception as err: # model is optional: the game survives without it
 
57
 
58
  # ------------------------------------------------------------ the Warden brain
59
 
60
+ # Prefer the Warden shipped inside the Space repo (pre-quantized nf4, no
61
+ # download at boot); fall back to the hub for self-hosters who clone without
62
+ # the LFS weights.
63
+ _SHIPPED = REPO_ROOT / "model"
64
+ MODEL_ID = os.environ.get("WARDEN_MODEL") or (
65
+ str(_SHIPPED)
66
+ if (_SHIPPED / "config.json").exists()
67
+ else "nvidia/NVIDIA-Nemotron-3-Nano-30B-A3B-BF16"
68
+ )
69
  INTERNAL_KEY = os.environ.get("SCRYPT_INTERNAL_KEY") or secrets.token_hex(16)
70
 
71
  tok = None
 
135
 
136
  tok = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
137
  # 4-bit fits the `large` (48GB) slice and matches the Q4 GGUF the
138
+ # local game ships — quality parity, not a downgrade. The shipped
139
+ # model/ dir is already nf4 (config carries quantization_config);
140
+ # only quantize on the fly when loading raw BF16 from the hub.
141
+ _cfg = Path(MODEL_ID) / "config.json"
142
+ _already_quant = _cfg.exists() and "quantization_config" in _cfg.read_text()
143
+ _quant_kwargs = (
144
+ {}
145
+ if _already_quant
146
+ else {
147
+ "quantization_config": BitsAndBytesConfig(
148
+ load_in_4bit=True,
149
+ bnb_4bit_quant_type="nf4",
150
+ bnb_4bit_compute_dtype=torch.bfloat16,
151
+ )
152
+ }
153
+ )
154
  model = AutoModelForCausalLM.from_pretrained(
155
  MODEL_ID,
156
  trust_remote_code=True,
 
 
 
 
 
157
  device_map="cuda",
158
+ **_quant_kwargs,
159
  )
160
  WARDEN_ERR = ""
161
  except Exception as err: # model is optional: the game survives without it