File size: 10,443 Bytes
f16c8fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af7c75f
 
 
 
 
f16c8fc
 
 
 
 
 
 
 
 
 
 
 
 
af7c75f
 
f16c8fc
 
 
 
af7c75f
 
f16c8fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af7c75f
f16c8fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
ESCTR Training Script β€” GRPO with TRL + vLLM
=============================================

Train an LLM to be an autonomous financial controller using
Group Relative Policy Optimization (GRPO) against the ESCTR environment.

Usage (Colab / HF Jobs):
    pip install -Uq "trl[vllm]" trackio datasets
    pip install -e .        # install esctr-environment package
    python train.py

The environment runs in-process (no HTTP server needed during training).
The HF Space deployment is only for judges to test the environment interactively.
"""

import random
import sys
import os

os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"

from datasets import Dataset
from trl import GRPOConfig, GRPOTrainer

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


# ---------------------------------------------------------------------------
# System prompt β€” tells the model what it is and what tools are available
# ---------------------------------------------------------------------------
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."""


# ---------------------------------------------------------------------------
# ESCTR Environment wrapper for TRL environment_factory
# ---------------------------------------------------------------------------
# TRL discovers public methods (with docstrings) as callable tools.
# The model generates tool calls; TRL executes them and feeds results back.
# ---------------------------------------------------------------------------

# Task to train on β€” start with the easiest task for stable training
TRAIN_TASKS = [
    t.strip()
    for t in os.environ.get("ESCTR_TASKS", os.environ.get("ESCTR_TASK", "procurement_reconciliation")).split(",")
    if t.strip()
]


class ESCTRToolEnv:
    """TRL-compatible wrapper around the ESCTR environment.

    Public methods with docstrings are auto-discovered as tools by TRL's
    environment_factory. The trainer handles the multi-turn loop automatically.
    """

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

    def reset(self, **kwargs) -> str | None:
        """Reset the environment and return the initial briefing."""
        seed = random.randint(0, 100_000)
        # Simple curriculum: sample tasks from configured task pool.
        self._task = random.choice(self._tasks)
        obs = self.env.reset(
            task_name=self._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:
            raise ValueError("Episode is over. No more actions allowed.")

        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:
            raise ValueError("Episode is over. No more actions allowed.")

        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:
            raise ValueError("Episode is over. No more actions allowed.")

        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:
            raise ValueError("Episode is over. No more actions allowed.")

        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 β€” reads from env instances after each episode
# ---------------------------------------------------------------------------

def reward_func(environments, **kwargs) -> list[float]:
    """Extract reward from each environment instance after episode completion."""
    return [env.reward for env in environments]


# ---------------------------------------------------------------------------
# Training configuration
# ---------------------------------------------------------------------------

def main():
    # Model selection β€” Qwen3-1.7B is efficient on T4 GPU
    model_name = os.environ.get("ESCTR_MODEL", "Qwen/Qwen3-1.7B")
    output_dir = os.environ.get("ESCTR_OUTPUT", "esctr-grpo-trained")
    num_episodes = int(os.environ.get("ESCTR_EPISODES", "1000"))

    # Create dataset β€” each entry triggers one rollout episode
    dataset = Dataset.from_dict({
        "prompt": [[{"role": "user", "content": SYSTEM_PROMPT}]] * num_episodes
    })

    # GRPO configuration
    grpo_config = GRPOConfig(
        # Training schedule
        num_train_epochs=1,
        learning_rate=1e-6,
        gradient_accumulation_steps=4,
        per_device_train_batch_size=1,
        warmup_steps=10,
        optim="adamw_torch",
        max_grad_norm=1.0,

        # GRPO settings
        num_generations=2,
        max_completion_length=768,
        log_completions=True,
        num_completions_to_print=2,
        chat_template_kwargs={"enable_thinking": False},

        # Logging
        output_dir=output_dir,
        report_to="trackio",
        trackio_space_id=output_dir,
        logging_steps=1,
        save_steps=25,
        save_total_limit=2,

        # Memory optimization
        gradient_checkpointing=True,
        bf16=False,
        fp16=True,

        # Hub integration
        push_to_hub=True,
    )

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

    # Show GPU stats before training
    import torch
    if torch.cuda.is_available():
        gpu_stats = torch.cuda.get_device_properties(0)
        start_gpu_memory = round(torch.cuda.max_memory_reserved() / 1024 / 1024 / 1024, 3)
        max_memory = round(gpu_stats.total_memory / 1024 / 1024 / 1024, 3)
        print(f"GPU = {gpu_stats.name}. Max memory = {max_memory} GB.")
        print(f"{start_gpu_memory} GB of memory reserved.")

    print(f"\n{'='*60}")
    print(f"ESCTR Training β€” {model_name}")
    print(f"Tasks: {', '.join(TRAIN_TASKS)}")
    print(f"Episodes: {num_episodes}")
    print(f"Output: {output_dir}")
    print(f"{'='*60}\n")

    # Train!
    trainer_stats = trainer.train()

    # Show training stats
    if torch.cuda.is_available():
        used_memory = round(torch.cuda.max_memory_reserved() / 1024 / 1024 / 1024, 3)
        print(f"\nTraining completed in {trainer_stats.metrics['train_runtime']:.0f} seconds")
        print(f"Peak GPU memory: {used_memory} GB / {max_memory} GB")

    # Save and push
    trainer.save_model(output_dir)
    trainer.push_to_hub()
    print(f"\nModel saved to {output_dir} and pushed to Hub!")


if __name__ == "__main__":
    main()