Spaces:
Sleeping
Sleeping
Pranjal Parashar commited on
Improve eval fallback and add Unsloth eval path
Browse files- baseline.py +0 -1
- eval.py +83 -7
- pyproject.toml +3 -0
baseline.py
CHANGED
|
@@ -1111,7 +1111,6 @@ def fallback_action(task_name: str, observation: Any) -> dict[str, Any]:
|
|
| 1111 |
if grader_family == "simulation":
|
| 1112 |
read_order = [
|
| 1113 |
"docs/runtime_contract.md",
|
| 1114 |
-
"templates/report_pipeline_template.yaml",
|
| 1115 |
"data/sales_orders.csv",
|
| 1116 |
]
|
| 1117 |
for path in read_order:
|
|
|
|
| 1111 |
if grader_family == "simulation":
|
| 1112 |
read_order = [
|
| 1113 |
"docs/runtime_contract.md",
|
|
|
|
| 1114 |
"data/sales_orders.csv",
|
| 1115 |
]
|
| 1116 |
for path in read_order:
|
eval.py
CHANGED
|
@@ -3,10 +3,13 @@
|
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
import argparse
|
|
|
|
|
|
|
| 6 |
import statistics
|
| 7 |
from typing import Any
|
| 8 |
|
| 9 |
try:
|
|
|
|
| 10 |
from .training_utils import (
|
| 11 |
DEFAULT_MAX_NEW_TOKENS,
|
| 12 |
DEFAULT_MAX_TURNS,
|
|
@@ -18,6 +21,7 @@ try:
|
|
| 18 |
run_episode,
|
| 19 |
)
|
| 20 |
except ImportError:
|
|
|
|
| 21 |
from training_utils import (
|
| 22 |
DEFAULT_MAX_NEW_TOKENS,
|
| 23 |
DEFAULT_MAX_TURNS,
|
|
@@ -29,20 +33,41 @@ except ImportError:
|
|
| 29 |
run_episode,
|
| 30 |
)
|
| 31 |
|
|
|
|
|
|
|
| 32 |
|
| 33 |
def parse_args() -> argparse.Namespace:
|
| 34 |
parser = argparse.ArgumentParser(description="Evaluate FlowOS policies before and after GRPO fine-tuning")
|
| 35 |
parser.add_argument("--model-id", default=DEFAULT_TRAIN_MODEL, help="Base model id")
|
| 36 |
parser.add_argument("--checkpoint-path", default=None, help="Optional fine-tuned checkpoint or LoRA adapter path")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
parser.add_argument("--env-url", default="http://localhost:7860", help="FlowOS OpenEnv server URL")
|
| 38 |
parser.add_argument("--task-scope", default="all", help="Task scope: all or comma-separated task ids")
|
| 39 |
parser.add_argument("--episodes", type=int, default=12, help="Total evaluation episodes per policy")
|
| 40 |
parser.add_argument("--max-turns", type=int, default=DEFAULT_MAX_TURNS, help="Max environment turns per episode")
|
| 41 |
parser.add_argument("--max-new-tokens", type=int, default=DEFAULT_MAX_NEW_TOKENS, help="Max tokens per action generation")
|
| 42 |
parser.add_argument("--temperature", type=float, default=0.0, help="Sampling temperature for evaluation")
|
|
|
|
|
|
|
|
|
|
| 43 |
return parser.parse_args()
|
| 44 |
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
def apply_chat_template(tokenizer: Any, messages: list[dict[str, str]]) -> str:
|
| 47 |
try:
|
| 48 |
return tokenizer.apply_chat_template(
|
|
@@ -59,7 +84,27 @@ def apply_chat_template(tokenizer: Any, messages: list[dict[str, str]]) -> str:
|
|
| 59 |
)
|
| 60 |
|
| 61 |
|
| 62 |
-
def load_policy(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
import torch
|
| 64 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 65 |
|
|
@@ -95,7 +140,7 @@ def generate_action(
|
|
| 95 |
user_prompt: str,
|
| 96 |
max_new_tokens: int,
|
| 97 |
temperature: float,
|
| 98 |
-
) -> dict[str, Any] | None:
|
| 99 |
import torch
|
| 100 |
|
| 101 |
prompt_text = apply_chat_template(
|
|
@@ -118,7 +163,7 @@ def generate_action(
|
|
| 118 |
)
|
| 119 |
completion_ids = generated[0][inputs["input_ids"].shape[1] :]
|
| 120 |
text = tokenizer.decode(completion_ids, skip_special_tokens=True)
|
| 121 |
-
return parse_action_json(text)
|
| 122 |
|
| 123 |
|
| 124 |
def summarize(results: list[dict[str, Any]]) -> dict[str, float]:
|
|
@@ -143,14 +188,32 @@ def run_policy(
|
|
| 143 |
for sample in samples:
|
| 144 |
def policy(observation: Any, transcript: list[dict[str, Any]]) -> dict[str, Any]:
|
| 145 |
user_prompt = build_turn_prompt(observation, transcript)
|
| 146 |
-
action = generate_action(
|
| 147 |
model=model,
|
| 148 |
tokenizer=tokenizer,
|
| 149 |
user_prompt=user_prompt,
|
| 150 |
max_new_tokens=args.max_new_tokens,
|
| 151 |
temperature=args.temperature,
|
| 152 |
)
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
metrics = run_episode(
|
| 156 |
base_url=args.env_url,
|
|
@@ -191,14 +254,27 @@ def print_table(rows: list[dict[str, Any]]) -> None:
|
|
| 191 |
|
| 192 |
def main() -> None:
|
| 193 |
args = parse_args()
|
|
|
|
| 194 |
|
| 195 |
-
base_model, base_tokenizer, base_label = load_policy(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
rows: list[dict[str, Any]] = []
|
| 197 |
base_results = run_policy(base_label, base_model, base_tokenizer, args)
|
| 198 |
rows.append({"policy": base_label, **summarize(base_results)})
|
| 199 |
|
| 200 |
if args.checkpoint_path:
|
| 201 |
-
tuned_model, tuned_tokenizer, tuned_label = load_policy(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
tuned_results = run_policy(tuned_label, tuned_model, tuned_tokenizer, args)
|
| 203 |
rows.append({"policy": tuned_label, **summarize(tuned_results)})
|
| 204 |
|
|
|
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
import argparse
|
| 6 |
+
import logging
|
| 7 |
+
import re
|
| 8 |
import statistics
|
| 9 |
from typing import Any
|
| 10 |
|
| 11 |
try:
|
| 12 |
+
from .baseline import action_is_valid, fallback_action
|
| 13 |
from .training_utils import (
|
| 14 |
DEFAULT_MAX_NEW_TOKENS,
|
| 15 |
DEFAULT_MAX_TURNS,
|
|
|
|
| 21 |
run_episode,
|
| 22 |
)
|
| 23 |
except ImportError:
|
| 24 |
+
from baseline import action_is_valid, fallback_action
|
| 25 |
from training_utils import (
|
| 26 |
DEFAULT_MAX_NEW_TOKENS,
|
| 27 |
DEFAULT_MAX_TURNS,
|
|
|
|
| 33 |
run_episode,
|
| 34 |
)
|
| 35 |
|
| 36 |
+
logger = logging.getLogger(__name__)
|
| 37 |
+
|
| 38 |
|
| 39 |
def parse_args() -> argparse.Namespace:
|
| 40 |
parser = argparse.ArgumentParser(description="Evaluate FlowOS policies before and after GRPO fine-tuning")
|
| 41 |
parser.add_argument("--model-id", default=DEFAULT_TRAIN_MODEL, help="Base model id")
|
| 42 |
parser.add_argument("--checkpoint-path", default=None, help="Optional fine-tuned checkpoint or LoRA adapter path")
|
| 43 |
+
parser.add_argument(
|
| 44 |
+
"--backend",
|
| 45 |
+
default="transformers",
|
| 46 |
+
choices=("transformers", "unsloth"),
|
| 47 |
+
help="Model loading backend for evaluation",
|
| 48 |
+
)
|
| 49 |
parser.add_argument("--env-url", default="http://localhost:7860", help="FlowOS OpenEnv server URL")
|
| 50 |
parser.add_argument("--task-scope", default="all", help="Task scope: all or comma-separated task ids")
|
| 51 |
parser.add_argument("--episodes", type=int, default=12, help="Total evaluation episodes per policy")
|
| 52 |
parser.add_argument("--max-turns", type=int, default=DEFAULT_MAX_TURNS, help="Max environment turns per episode")
|
| 53 |
parser.add_argument("--max-new-tokens", type=int, default=DEFAULT_MAX_NEW_TOKENS, help="Max tokens per action generation")
|
| 54 |
parser.add_argument("--temperature", type=float, default=0.0, help="Sampling temperature for evaluation")
|
| 55 |
+
parser.add_argument("--load-in-4bit", action="store_true", help="Use 4-bit quantized loading when supported")
|
| 56 |
+
parser.add_argument("--max-seq-length", type=int, default=4096, help="Max sequence length for model loading")
|
| 57 |
+
parser.add_argument("--debug-actions", action="store_true", help="Log raw completions and fallback actions")
|
| 58 |
return parser.parse_args()
|
| 59 |
|
| 60 |
|
| 61 |
+
def _clean_preview(text: str, limit: int = 220) -> str:
|
| 62 |
+
normalized = text.replace("\u00a0", " ")
|
| 63 |
+
normalized = re.sub(r"\s+", " ", normalized).strip()
|
| 64 |
+
if not normalized:
|
| 65 |
+
return "<empty>"
|
| 66 |
+
if len(normalized) > limit:
|
| 67 |
+
return normalized[:limit] + "..."
|
| 68 |
+
return normalized
|
| 69 |
+
|
| 70 |
+
|
| 71 |
def apply_chat_template(tokenizer: Any, messages: list[dict[str, str]]) -> str:
|
| 72 |
try:
|
| 73 |
return tokenizer.apply_chat_template(
|
|
|
|
| 84 |
)
|
| 85 |
|
| 86 |
|
| 87 |
+
def load_policy(
|
| 88 |
+
model_id: str,
|
| 89 |
+
checkpoint_path: str | None,
|
| 90 |
+
backend: str,
|
| 91 |
+
load_in_4bit: bool,
|
| 92 |
+
max_seq_length: int,
|
| 93 |
+
) -> tuple[Any, Any, str]:
|
| 94 |
+
if backend == "unsloth":
|
| 95 |
+
from unsloth import FastLanguageModel
|
| 96 |
+
|
| 97 |
+
model_name = checkpoint_path or model_id
|
| 98 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 99 |
+
model_name=model_name,
|
| 100 |
+
max_seq_length=max_seq_length,
|
| 101 |
+
dtype=None,
|
| 102 |
+
load_in_4bit=load_in_4bit,
|
| 103 |
+
)
|
| 104 |
+
FastLanguageModel.for_inference(model)
|
| 105 |
+
label = "tuned" if checkpoint_path else "base"
|
| 106 |
+
return model, tokenizer, label
|
| 107 |
+
|
| 108 |
import torch
|
| 109 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 110 |
|
|
|
|
| 140 |
user_prompt: str,
|
| 141 |
max_new_tokens: int,
|
| 142 |
temperature: float,
|
| 143 |
+
) -> tuple[dict[str, Any] | None, str]:
|
| 144 |
import torch
|
| 145 |
|
| 146 |
prompt_text = apply_chat_template(
|
|
|
|
| 163 |
)
|
| 164 |
completion_ids = generated[0][inputs["input_ids"].shape[1] :]
|
| 165 |
text = tokenizer.decode(completion_ids, skip_special_tokens=True)
|
| 166 |
+
return parse_action_json(text), text
|
| 167 |
|
| 168 |
|
| 169 |
def summarize(results: list[dict[str, Any]]) -> dict[str, float]:
|
|
|
|
| 188 |
for sample in samples:
|
| 189 |
def policy(observation: Any, transcript: list[dict[str, Any]]) -> dict[str, Any]:
|
| 190 |
user_prompt = build_turn_prompt(observation, transcript)
|
| 191 |
+
action, raw_text = generate_action(
|
| 192 |
model=model,
|
| 193 |
tokenizer=tokenizer,
|
| 194 |
user_prompt=user_prompt,
|
| 195 |
max_new_tokens=args.max_new_tokens,
|
| 196 |
temperature=args.temperature,
|
| 197 |
)
|
| 198 |
+
if args.debug_actions:
|
| 199 |
+
logger.info(
|
| 200 |
+
"Eval completion task=%s step=%s text=%s parsed=%s",
|
| 201 |
+
sample.task_id,
|
| 202 |
+
observation.step_count + 1,
|
| 203 |
+
_clean_preview(raw_text),
|
| 204 |
+
action,
|
| 205 |
+
)
|
| 206 |
+
if not action_is_valid(action, observation):
|
| 207 |
+
fallback = fallback_action(sample.task_id, observation)
|
| 208 |
+
if args.debug_actions:
|
| 209 |
+
logger.info(
|
| 210 |
+
"Eval fallback task=%s step=%s fallback=%s",
|
| 211 |
+
sample.task_id,
|
| 212 |
+
observation.step_count + 1,
|
| 213 |
+
fallback,
|
| 214 |
+
)
|
| 215 |
+
action = fallback
|
| 216 |
+
return {"action": action, "metadata": {"text": raw_text}}
|
| 217 |
|
| 218 |
metrics = run_episode(
|
| 219 |
base_url=args.env_url,
|
|
|
|
| 254 |
|
| 255 |
def main() -> None:
|
| 256 |
args = parse_args()
|
| 257 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
|
| 258 |
|
| 259 |
+
base_model, base_tokenizer, base_label = load_policy(
|
| 260 |
+
args.model_id,
|
| 261 |
+
None,
|
| 262 |
+
args.backend,
|
| 263 |
+
args.load_in_4bit,
|
| 264 |
+
args.max_seq_length,
|
| 265 |
+
)
|
| 266 |
rows: list[dict[str, Any]] = []
|
| 267 |
base_results = run_policy(base_label, base_model, base_tokenizer, args)
|
| 268 |
rows.append({"policy": base_label, **summarize(base_results)})
|
| 269 |
|
| 270 |
if args.checkpoint_path:
|
| 271 |
+
tuned_model, tuned_tokenizer, tuned_label = load_policy(
|
| 272 |
+
args.model_id,
|
| 273 |
+
args.checkpoint_path,
|
| 274 |
+
args.backend,
|
| 275 |
+
args.load_in_4bit,
|
| 276 |
+
args.max_seq_length,
|
| 277 |
+
)
|
| 278 |
tuned_results = run_policy(tuned_label, tuned_model, tuned_tokenizer, args)
|
| 279 |
rows.append({"policy": tuned_label, **summarize(tuned_results)})
|
| 280 |
|
pyproject.toml
CHANGED
|
@@ -28,6 +28,9 @@ train = [
|
|
| 28 |
"transformers>=4.45.0",
|
| 29 |
"trl>=0.11.0",
|
| 30 |
]
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
[project.scripts]
|
| 33 |
server = "developer_control_room.server.app:main"
|
|
|
|
| 28 |
"transformers>=4.45.0",
|
| 29 |
"trl>=0.11.0",
|
| 30 |
]
|
| 31 |
+
unsloth = [
|
| 32 |
+
"unsloth",
|
| 33 |
+
]
|
| 34 |
|
| 35 |
[project.scripts]
|
| 36 |
server = "developer_control_room.server.app:main"
|