File size: 15,542 Bytes
08a3b81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/usr/bin/env python3
"""
ESCTR 4B Training β€” GRPO + LoRA on RTX 4090 (24 GB)
====================================================

Self-contained training script. No imports from other train scripts.

Memory budget (RTX 4090, 24 GB):
    Qwen3-4B in bf16       β‰ˆ  8 GB
    LoRA adapters           β‰ˆ  0.05 GB
    KV cache (K=2, 512 tok) β‰ˆ  3 GB
    Grad checkpointing      β‰ˆ  3 GB
    Optimizer (LoRA only)   β‰ˆ  0.2 GB
    ──────────────────────────────────
    Total                   β‰ˆ 14 GB   (plenty of headroom)

Usage on RunPod:
    chmod +x setup_runpod.sh && ./setup_runpod.sh
    python train_4b.py
"""

import os
import sys
import random
import time

# ── Memory + cache config (must be set BEFORE any torch import) ───────────
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"

# Redirect HF + torch caches to the CONTAINER DISK (not /workspace volume,
# which is often only 5 GB on RunPod β€” too small for 4B model weights).
# The container disk is typically 50 GB at /.
cache_root = "/root" if os.path.isdir("/root") else "."
os.environ.setdefault("HF_HOME", os.path.join(cache_root, "hf_cache"))
os.environ.setdefault("TORCH_HOME", os.path.join(cache_root, "torch_cache"))
os.makedirs(os.environ["HF_HOME"], exist_ok=True)
os.makedirs(os.environ["TORCH_HOME"], exist_ok=True)

import torch
from datasets import Dataset
from peft import LoraConfig
from trl import GRPOConfig, GRPOTrainer

# ── Import ESCTR environment (in-process, no server) ─────────────────────
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from server.environment import ESCTREnvironment
from server.models import ESCTRAction


# ── System prompt ─────────────────────────────────────────────────────────
SYSTEM_PROMPT = """You are an autonomous Financial Controller AI operating within an enterprise ERP system.

Your job is to investigate financial discrepancies in procurement records by using the available tools, then submit a precise monetary adjustment.

INVESTIGATION WORKFLOW:
1. Query databases to discover what records exist (purchase_orders, invoices, shipping_logs, sla_contracts, warehouse_logs)
2. Read specific documents to get full details
3. Compare line items, delivery dates, and contract terms
4. Calculate the exact adjustment amount
5. Submit your financial decision with the calculated amount and reasoning

CRITICAL RULES:
- Always query AND read documents before submitting. Never guess.
- Your adjustment_amount must be the EXACT monetary difference you calculated.
- Show your arithmetic in the adjustment_reason.
- If a vendor offers a settlement, verify their claims against internal records before accepting.

You have access to the following tools. Call them to interact with the ERP system."""


# ── Task config ───────────────────────────────────────────────────────────
TRAIN_TASKS = [
    t.strip()
    for t in os.environ.get(
        "ESCTR_TASKS",
        os.environ.get("ESCTR_TASK", "procurement_reconciliation")
    ).split(",")
    if t.strip()
]


# ── TRL environment wrapper ──────────────────────────────────────────────
class ESCTRToolEnv:
    """TRL-compatible wrapper.

    Public methods with docstrings become tools. TRL handles the multi-turn
    loop automatically via environment_factory.
    """

    def __init__(self):
        self.env = ESCTREnvironment()
        self.reward = 0.0
        self.done = False
        self._tasks = TRAIN_TASKS or ["procurement_reconciliation"]

    def reset(self, **kwargs) -> str | None:
        """Reset the environment and return the initial briefing."""
        seed = random.randint(0, 100_000)
        task = random.choice(self._tasks)
        obs = self.env.reset(task_name=task, seed=seed)
        self.reward = 0.0
        self.done = False
        return obs.system_response

    def query_database(self, table: str) -> str:
        """
        Query a corporate database table to discover available records.

        Args:
            table: The database table to query. One of: 'purchase_orders', 'invoices', 'shipping_logs', 'sla_contracts', 'warehouse_logs'

        Returns:
            A summary of records found in the specified table.
        """
        if self.done:
            return "Episode is over."
        action = ESCTRAction(action_type="query_database", query_parameters={"table": table})
        obs = self.env.step(action)
        self.reward = obs.reward
        self.done = obs.done
        return obs.system_response

    def read_document(self, document_id: str) -> str:
        """
        Read a specific document by its unique identifier to see full details.

        Args:
            document_id: The document ID to read, e.g. 'PO-2024-0055' or 'INV-2024-0055'

        Returns:
            The full contents of the requested document.
        """
        if self.done:
            return "Episode is over."
        action = ESCTRAction(action_type="read_document", document_id=document_id)
        obs = self.env.step(action)
        self.reward = obs.reward
        self.done = obs.done
        return obs.system_response

    def communicate_vendor(self, message_content: str) -> str:
        """
        Send a message to the vendor during a dispute negotiation.

        Args:
            message_content: The message to send to the vendor, such as requesting clarification or rejecting a settlement offer.

        Returns:
            The vendor's response to your message.
        """
        if self.done:
            return "Episode is over."
        action = ESCTRAction(action_type="communicate_vendor", message_content=message_content)
        obs = self.env.step(action)
        self.reward = obs.reward
        self.done = obs.done
        return obs.system_response

    def submit_financial_decision(self, adjustment_amount: float, adjustment_reason: str) -> str:
        """
        Submit the final financial adjustment. This is the terminal action that ends the episode.

        Args:
            adjustment_amount: The exact monetary adjustment amount as a float (e.g. 450.00). Must be calculated from the documents.
            adjustment_reason: A brief explanation of why this adjustment is correct, including your arithmetic.

        Returns:
            The grading result with your score and feedback.
        """
        if self.done:
            return "Episode is over."
        action = ESCTRAction(
            action_type="submit_financial_decision",
            adjustment_amount=adjustment_amount,
            adjustment_reason=adjustment_reason,
        )
        obs = self.env.step(action)
        self.reward = obs.reward
        self.done = obs.done
        return obs.system_response


# ── Reward function ───────────────────────────────────────────────────────
def reward_func(environments, **kwargs) -> list[float]:
    """Shaped reward for GRPO β€” gives partial credit for investigation progress.

    Without shaping, the model must call submit_financial_decision to get ANY
    reward. Qwen3-4B never calls submit (it stops after investigating), so
    all rewards are 0, GRPO has zero gradient, and nothing is learned.

    Shaping:
        - Each tool call earns a small bonus (0.05)
        - Calling submit earns a larger bonus (0.15) regardless of correctness
        - The environment's graded reward (0-1) is added on top
        - This creates variance between rollouts even without submission
    """
    rewards = []
    for env in environments:
        # Base: the environment's graded reward (non-zero only if submitted)
        r = env.reward

        # Shaping: credit for investigation effort
        step_count = env.env._state.step_count if hasattr(env.env, '_state') else 0
        submitted = env.env._state.outcome_submitted if hasattr(env.env, '_state') else False

        # Small per-step bonus for using tools (caps at 0.20)
        investigation_bonus = min(step_count * 0.05, 0.20)

        # Bonus for actually submitting (even with wrong amount)
        submit_bonus = 0.15 if submitted else 0.0

        rewards.append(r + investigation_bonus + submit_bonus)
    return rewards


# ── Main ──────────────────────────────────────────────────────────────────
def main():
    # ── User-configurable via env vars ────────────────────────────────────
    model_name = os.environ.get("ESCTR_MODEL", "Qwen/Qwen3-4B")
    num_episodes = int(os.environ.get("ESCTR_EPISODES", "300"))
    max_len = int(os.environ.get("ESCTR_MAX_COMPLETION_LENGTH", "1024"))
    lora_r = int(os.environ.get("ESCTR_LORA_R", "16"))
    grad_accum = int(os.environ.get("ESCTR_GRAD_ACCUM", "4"))

    # Output goes to container disk (not /workspace which may be tiny)
    default_out = "/root/esctr-4b-lora" if os.path.isdir("/root") else "./esctr-4b-lora"
    output_dir = os.environ.get("ESCTR_OUTPUT", default_out)
    os.makedirs(output_dir, exist_ok=True)

    # ── Preflight checks ─────────────────────────────────────────────────
    assert torch.cuda.is_available(), "CUDA not available β€” this script requires a GPU."
    gpu = torch.cuda.get_device_properties(0)
    vram_gb = round(gpu.total_memory / 1024**3, 1)
    print(f"\n{'='*60}")
    print(f"  GPU: {gpu.name}  |  VRAM: {vram_gb} GB")
    print(f"  Model: {model_name}")
    print(f"  Tasks: {', '.join(TRAIN_TASKS)}")
    print(f"  Episodes: {num_episodes}")
    print(f"  LoRA rank: {lora_r}  |  Max completion: {max_len} tokens")
    print(f"  Output: {output_dir}")
    print(f"{'='*60}\n")

    # ── Dataset ───────────────────────────────────────────────────────────
    dataset = Dataset.from_dict({
        "prompt": [[{"role": "user", "content": SYSTEM_PROMPT}]] * num_episodes
    })

    # ── LoRA config ───────────────────────────────────────────────────────
    # Target all linear layers for maximum expressiveness within budget.
    # TRL automatically uses base model as reference when peft_config is set,
    # so we do NOT need a separate ref model copy in VRAM.
    peft_config = LoraConfig(
        task_type="CAUSAL_LM",
        r=lora_r,
        lora_alpha=lora_r * 2,       # standard 2x multiplier
        target_modules="all-linear",  # TRL recommended
        lora_dropout=0.05,
        bias="none",
    )

    # ── Detect precision ──────────────────────────────────────────────────
    # RTX 4090 (Ada Lovelace) has native bf16 support.
    # Fall back to fp16 if something is wrong.
    use_bf16 = os.environ.get("ESCTR_BF16", "1") == "1"
    try:
        if use_bf16:
            torch.tensor([1.0], dtype=torch.bfloat16, device="cuda")
    except Exception:
        print("⚠️  bf16 not supported on this GPU, falling back to fp16")
        use_bf16 = False

    # ── GRPO config ───────────────────────────────────────────────────────
    grpo_config = GRPOConfig(
        # Schedule
        num_train_epochs=1,
        learning_rate=2e-5,          # higher LR for LoRA (only adapter weights)
        warmup_steps=10,
        max_grad_norm=1.0,
        optim="adamw_torch",

        # Batching β€” keep batch=1, accumulate for effective batch
        per_device_train_batch_size=1,
        gradient_accumulation_steps=grad_accum,

        # GRPO β€” exploration is CRITICAL
        # Without high temperature, Qwen3-4B collapses to entropy ~0.0005
        # (deterministic) and both K rollouts get identical reward β†’ zero gradient.
        num_generations=4,           # K=4: more rollouts = more reward variance
        max_completion_length=max_len,
        temperature=1.5,             # Force exploration (default 1.0 is too greedy for 4B)
        max_tool_calling_iterations=10,  # Allow enough turns for investigate + submit
        log_completions=True,
        num_completions_to_print=1,

        # CRITICAL for Qwen3: disable thinking mode or it burns all tokens
        # on <think>...</think> blocks and never makes tool calls
        chat_template_kwargs={"enable_thinking": False},

        # Memory
        gradient_checkpointing=True,
        bf16=use_bf16,
        fp16=not use_bf16,

        # Logging β€” "none" avoids Trackio integration crashes (known issue)
        output_dir=output_dir,
        report_to="none",
        logging_steps=1,
        save_steps=50,
        save_total_limit=2,

        # Do NOT push to hub during training (avoids quota/auth crashes)
        push_to_hub=False,
    )

    # ── Create trainer ────────────────────────────────────────────────────
    print("Loading model + LoRA adapters...")
    t0 = time.time()

    trainer = GRPOTrainer(
        model=model_name,
        reward_funcs=reward_func,
        train_dataset=dataset,
        args=grpo_config,
        environment_factory=ESCTRToolEnv,
        peft_config=peft_config,
    )

    load_time = time.time() - t0
    used = round(torch.cuda.max_memory_reserved() / 1024**3, 2)
    print(f"βœ… Model loaded in {load_time:.0f}s  |  VRAM used: {used} GB / {vram_gb} GB\n")

    # ── Train! ────────────────────────────────────────────────────────────
    print("πŸš€ Starting GRPO training...\n")
    t0 = time.time()
    stats = trainer.train()
    elapsed = time.time() - t0

    peak = round(torch.cuda.max_memory_reserved() / 1024**3, 2)
    print(f"\n{'='*60}")
    print(f"  βœ… Training complete!")
    print(f"  Wall time: {elapsed/60:.1f} minutes")
    print(f"  Peak VRAM: {peak} GB / {vram_gb} GB")
    if hasattr(stats, 'metrics'):
        rt = stats.metrics.get('train_runtime', elapsed)
        print(f"  Train runtime: {rt:.0f}s")
    print(f"{'='*60}\n")

    # ── Save ──────────────────────────────────────────────────────────────
    save_path = os.path.join(output_dir, "final")
    trainer.save_model(save_path)
    print(f"πŸ’Ύ LoRA adapters saved to: {save_path}")
    print(f"   (To push to Hub later: trainer.push_to_hub())")


if __name__ == "__main__":
    main()