rishavutk commited on
Commit
c66fcbe
·
verified ·
1 Parent(s): 83bf1ef

Fix adapter eval for 3B notebooks

Browse files
scripts/hf_eval_supplymind_adapters.py CHANGED
@@ -25,7 +25,7 @@ from typing import Any
25
  import torch
26
  from huggingface_hub import snapshot_download
27
  from peft import PeftModel
28
- from transformers import AutoModelForCausalLM, AutoTokenizer
29
 
30
 
31
  REPO_ID = "rishavutk/supplymind"
@@ -46,6 +46,7 @@ def parse_args() -> argparse.Namespace:
46
  parser.add_argument("--seeds", default="101,113,127")
47
  parser.add_argument("--max-new-tokens", type=int, default=256)
48
  parser.add_argument("--model-id", default=MODEL_ID)
 
49
  return parser.parse_args()
50
 
51
 
@@ -117,13 +118,19 @@ def compact_observation(observation: Any, role: str, warehouse_id: str | None =
117
  def system_prompt(role: str) -> str:
118
  if role == "center":
119
  return (
120
- "You are the center policy in SupplyMind. Return only strict JSON matching CenterAction. "
121
- "Use central_procurements, central_liquidations, central_replenishments, inventory_transfer_proposals, offer_matches."
 
 
 
122
  )
123
  return (
124
- "You are the shared warehouse policy in SupplyMind. You control exactly one warehouse from the user observation. "
125
- "Return only strict JSON matching WarehouseAction with keys order_decisions, inventory_offers, inventory_requests, "
126
- "transfer_responses, and local_priority. Only use visible order_id and proposal_id values."
 
 
 
127
  )
128
 
129
 
@@ -195,10 +202,25 @@ def action_stats(role: str, payload: dict[str, Any] | None) -> dict[str, int]:
195
  return center_action_stats(payload) if role == "center" else warehouse_action_stats(payload)
196
 
197
 
198
- def load_model(model_id: str, adapter_id: str | None = None) -> tuple[Any, Any]:
199
  tokenizer = AutoTokenizer.from_pretrained(model_id)
200
- model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype="auto", device_map="auto")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
  if adapter_id:
 
202
  model = PeftModel.from_pretrained(model, adapter_id)
203
  model.eval()
204
  return model, tokenizer
@@ -324,7 +346,7 @@ def main() -> None:
324
  seeds = [int(value.strip()) for value in args.seeds.split(",") if value.strip()]
325
 
326
  log("loading_base_model")
327
- base_model, tokenizer = load_model(args.model_id)
328
  base = evaluate(args.role, "base", base_model, tokenizer, args.task_id, seeds, args.max_new_tokens)
329
  del base_model
330
  if torch.cuda.is_available():
@@ -340,7 +362,7 @@ def main() -> None:
340
  adapter_specs.append(("grpo", args.grpo_adapter_id))
341
  for label, adapter_id in adapter_specs:
342
  log("loading_adapter_model", label=label, adapter_id=adapter_id)
343
- adapter_model, tokenizer = load_model(args.model_id, adapter_id)
344
  evaluations[label] = evaluate(args.role, label, adapter_model, tokenizer, args.task_id, seeds, args.max_new_tokens)
345
  del adapter_model
346
  if torch.cuda.is_available():
 
25
  import torch
26
  from huggingface_hub import snapshot_download
27
  from peft import PeftModel
28
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
29
 
30
 
31
  REPO_ID = "rishavutk/supplymind"
 
46
  parser.add_argument("--seeds", default="101,113,127")
47
  parser.add_argument("--max-new-tokens", type=int, default=256)
48
  parser.add_argument("--model-id", default=MODEL_ID)
49
+ parser.add_argument("--load-in-4bit", action="store_true")
50
  return parser.parse_args()
51
 
52
 
 
118
  def system_prompt(role: str) -> str:
119
  if role == "center":
120
  return (
121
+ "You are the center policy in SupplyMind. Return only strict JSON matching CenterAction: "
122
+ "central_procurements, central_liquidations, central_replenishments, inventory_transfer_proposals, offer_matches. "
123
+ "Warehouses are controlled by a fixed heuristic. Earn margin and a small share of realized service profit, "
124
+ "but avoid waste, stockouts, overpriced actions, and needless shipments. Empty lists are only appropriate "
125
+ "when no useful procurement, liquidation, replenishment, transfer proposal, or offer match exists."
126
  )
127
  return (
128
+ "You are the shared warehouse policy in SupplyMind, copied across all warehouses. "
129
+ "You control exactly one warehouse from the user observation. Return only strict JSON matching WarehouseAction: "
130
+ "order_decisions, inventory_offers, inventory_requests, transfer_responses, and local_priority. "
131
+ "The center is controlled by a fixed heuristic. Accept orders you can serve, request needed stock, "
132
+ "and reject bad or impossible commitments. Only use order_id and proposal_id values visible in this observation. "
133
+ "Do not invent IDs, do not use markdown, and prefer fewer high-confidence actions over broad noisy actions."
134
  )
135
 
136
 
 
202
  return center_action_stats(payload) if role == "center" else warehouse_action_stats(payload)
203
 
204
 
205
+ def load_model(model_id: str, adapter_id: str | None = None, load_in_4bit: bool = False) -> tuple[Any, Any]:
206
  tokenizer = AutoTokenizer.from_pretrained(model_id)
207
+ quantization_config = None
208
+ if load_in_4bit:
209
+ compute_dtype = torch.bfloat16 if torch.cuda.is_available() and torch.cuda.is_bf16_supported() else torch.float16
210
+ quantization_config = BitsAndBytesConfig(
211
+ load_in_4bit=True,
212
+ bnb_4bit_quant_type="nf4",
213
+ bnb_4bit_use_double_quant=True,
214
+ bnb_4bit_compute_dtype=compute_dtype,
215
+ )
216
+ model = AutoModelForCausalLM.from_pretrained(
217
+ model_id,
218
+ torch_dtype="auto",
219
+ device_map="auto",
220
+ quantization_config=quantization_config,
221
+ )
222
  if adapter_id:
223
+ log("applying_adapter", adapter_id=adapter_id, load_in_4bit=load_in_4bit)
224
  model = PeftModel.from_pretrained(model, adapter_id)
225
  model.eval()
226
  return model, tokenizer
 
346
  seeds = [int(value.strip()) for value in args.seeds.split(",") if value.strip()]
347
 
348
  log("loading_base_model")
349
+ base_model, tokenizer = load_model(args.model_id, load_in_4bit=args.load_in_4bit)
350
  base = evaluate(args.role, "base", base_model, tokenizer, args.task_id, seeds, args.max_new_tokens)
351
  del base_model
352
  if torch.cuda.is_available():
 
362
  adapter_specs.append(("grpo", args.grpo_adapter_id))
363
  for label, adapter_id in adapter_specs:
364
  log("loading_adapter_model", label=label, adapter_id=adapter_id)
365
+ adapter_model, tokenizer = load_model(args.model_id, adapter_id, load_in_4bit=args.load_in_4bit)
366
  evaluations[label] = evaluate(args.role, label, adapter_model, tokenizer, args.task_id, seeds, args.max_new_tokens)
367
  del adapter_model
368
  if torch.cuda.is_available():