Skip to content

Commit 9438de5

Browse files
committed
changes
1 parent e69690a commit 9438de5

9 files changed

Lines changed: 248 additions & 167 deletions

File tree

examples/traces/basic-trace.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,40 @@
1+
import random
2+
13
import trackio
24

5+
PROJECT_ID = random.randint(100000, 999999)
6+
PROJECT_NAME = f"trace-demo-basic-{PROJECT_ID}"
7+
8+
examples = [
9+
("What is 2 + 2?", "2 + 2 = 4."),
10+
("What is the capital of Australia?", "The capital of Australia is Canberra."),
11+
(
12+
"Give me a one-sentence summary of Trackio.",
13+
"Trackio is a lightweight experiment tracking dashboard for ML and agent workflows.",
14+
),
15+
("Translate 'hello' to Spanish.", "Hola."),
16+
]
317

4-
trackio.init(project="trace-demo-basic")
18+
for run_idx in range(2):
19+
trackio.init(project=PROJECT_NAME, name=f"basic-run-{run_idx}")
520

6-
trackio.log(
7-
{
8-
"trace": trackio.Trace(
9-
messages=[
10-
{"role": "system", "content": "You are a concise assistant."},
11-
{"role": "user", "content": "What is 2 + 2?"},
12-
{"role": "assistant", "content": "2 + 2 = 4."},
13-
],
14-
metadata={"model_version": "demo-v1"},
21+
for step, (prompt, completion) in enumerate(examples):
22+
trackio.log(
23+
{
24+
"trace": trackio.Trace(
25+
messages=[
26+
{"role": "system", "content": "You are a concise assistant."},
27+
{"role": "user", "content": prompt},
28+
{"role": "assistant", "content": completion},
29+
],
30+
metadata={
31+
"model_version": f"demo-basic-v{run_idx + 1}",
32+
"trace_kind": "basic",
33+
"example_index": step,
34+
},
35+
)
36+
},
37+
step=step,
1538
)
16-
}
17-
)
1839

19-
trackio.finish()
40+
trackio.finish()

examples/traces/complex-trace.py

Lines changed: 63 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,74 @@
1+
import random
2+
13
import numpy as np
24

35
import trackio
46

7+
PROJECT_ID = random.randint(100000, 999999)
8+
PROJECT_NAME = f"trace-demo-complex-{PROJECT_ID}"
59

6-
trackio.init(project="trace-demo-complex")
710

8-
fake_screenshot = np.random.randint(0, 255, size=(240, 320, 3), dtype=np.uint8)
11+
def make_screenshot(seed: int):
12+
rng = np.random.default_rng(seed)
13+
return rng.integers(0, 255, size=(240, 320, 3), dtype=np.uint8)
914

10-
trackio.log(
11-
{
12-
"agent_trace": trackio.Trace(
13-
messages=[
14-
{"role": "system", "content": "You are a browser agent."},
15-
{
16-
"role": "user",
17-
"content": [
18-
{"type": "text", "text": "Inspect the page and summarize it."},
19-
trackio.Image(fake_screenshot, caption="browser screenshot"),
20-
],
21-
},
22-
{
23-
"role": "assistant",
24-
"content": "I will inspect the page and call a tool if needed.",
25-
"tool_calls": [
15+
16+
for run_idx in range(2):
17+
trackio.init(project=PROJECT_NAME, name=f"complex-run-{run_idx}")
18+
19+
for step in range(4):
20+
screenshot = make_screenshot(run_idx * 10 + step)
21+
trackio.log(
22+
{
23+
"agent_trace": trackio.Trace(
24+
messages=[
25+
{"role": "system", "content": "You are a browser agent."},
26+
{
27+
"role": "user",
28+
"content": [
29+
{
30+
"type": "text",
31+
"text": f"Inspect page variant {step} and summarize it.",
32+
},
33+
trackio.Image(
34+
screenshot,
35+
caption=f"browser screenshot run={run_idx} step={step}",
36+
),
37+
],
38+
},
39+
{
40+
"role": "assistant",
41+
"content": "I will inspect the page and call a tool if needed.",
42+
"tool_calls": [
43+
{
44+
"id": f"call_{run_idx}_{step}",
45+
"type": "function",
46+
"function": {
47+
"name": "extract_title",
48+
"arguments": '{"selector": "title"}',
49+
},
50+
}
51+
],
52+
},
53+
{
54+
"role": "tool",
55+
"content": f'{{"title": "Trackio Demo {run_idx}-{step}"}}',
56+
"tool_call_id": f"call_{run_idx}_{step}",
57+
},
2658
{
27-
"id": "call_1",
28-
"type": "function",
29-
"function": {
30-
"name": "extract_title",
31-
"arguments": '{"selector": "title"}',
32-
},
33-
}
59+
"role": "assistant",
60+
"content": f"The page variant {step} appears to be a Trackio demo with a visible screenshot and an extracted title.",
61+
},
3462
],
35-
},
36-
{
37-
"role": "tool",
38-
"content": '{"title": "Trackio Demo"}',
39-
"tool_call_id": "call_1",
40-
},
41-
{
42-
"role": "assistant",
43-
"content": "The page appears to be a Trackio demo with a visible screenshot and a simple title.",
44-
},
45-
],
46-
metadata={"model_version": "agent-preview", "environment": "browser"},
63+
metadata={
64+
"model_version": f"agent-preview-{run_idx}",
65+
"environment": "browser",
66+
"trace_kind": "complex",
67+
"step_variant": step,
68+
},
69+
)
70+
},
71+
step=step,
4772
)
48-
}
49-
)
5073

51-
trackio.finish()
74+
trackio.finish()
Lines changed: 107 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,121 @@
1-
from trl import GRPOConfig, GRPOTrainer
1+
# /// script
2+
# dependencies = [
3+
# "trackio",
4+
# "trl",
5+
# "datasets",
6+
# "transformers",
7+
# "torch",
8+
# ]
9+
# ///
10+
11+
import random
12+
13+
from datasets import Dataset
14+
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainerCallback
15+
from trl import SFTConfig, SFTTrainer
216

317
import trackio
418

19+
PROJECT_ID = random.randint(100000, 999999)
20+
PROJECT_NAME = f"trace-demo-trl-{PROJECT_ID}"
21+
MODEL_NAME = "sshleifer/tiny-gpt2"
22+
23+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
24+
if tokenizer.pad_token is None:
25+
tokenizer.pad_token = tokenizer.eos_token
26+
27+
examples = [
28+
{"prompt": "What is 2 + 2?", "completion": "2 + 2 = 4."},
29+
{
30+
"prompt": "What color is the sky on a clear day?",
31+
"completion": "The sky is typically blue on a clear day.",
32+
},
33+
{"prompt": "Translate 'good morning' to French.", "completion": "Bonjour."},
34+
{
35+
"prompt": "Name the capital of Japan.",
36+
"completion": "Tokyo is the capital of Japan.",
37+
},
38+
{
39+
"prompt": "Give one use of Trackio.",
40+
"completion": "Trackio can be used to inspect training logs and traces.",
41+
},
42+
]
43+
44+
45+
def format_example(example):
46+
return {
47+
"text": (
48+
"### Instruction:\n"
49+
f"{example['prompt']}\n\n"
50+
"### Response:\n"
51+
f"{example['completion']}"
52+
)
53+
}
54+
555

6-
trackio.init(project="trace-demo-trl")
56+
dataset = Dataset.from_list([format_example(example) for example in examples * 2])
757

858

9-
def log_rollouts(prompts, completions, rewards, step, model_version):
10-
trackio.log(
11-
{
12-
"traces": [
13-
trackio.Trace(
59+
class TraceLoggingCallback(TrainerCallback):
60+
def __init__(self, prompt_examples, run_label):
61+
self.prompt_examples = prompt_examples
62+
self.run_label = run_label
63+
64+
def on_log(self, args, state, control, logs=None, **kwargs):
65+
if not logs or state.global_step <= 0:
66+
return
67+
68+
sample = self.prompt_examples[
69+
(state.global_step - 1) % len(self.prompt_examples)
70+
]
71+
reward = max(0.0, 1.0 - float(logs.get("loss", 0.0)))
72+
trackio.log(
73+
{
74+
"trace": trackio.Trace(
1475
messages=[
15-
{"role": "user", "content": prompt},
16-
{"role": "assistant", "content": completion},
76+
{
77+
"role": "system",
78+
"content": "You are a supervised fine-tuning demo model.",
79+
},
80+
{"role": "user", "content": sample["prompt"]},
81+
{"role": "assistant", "content": sample["completion"]},
1782
],
1883
metadata={
19-
"reward": float(reward),
20-
"step": step,
21-
"model_version": model_version,
84+
"model_version": self.run_label,
85+
"trainer": "trl-sft",
86+
"loss": float(logs.get("loss", 0.0)),
87+
"reward": reward,
88+
"global_step": int(state.global_step),
2289
},
2390
)
24-
for prompt, completion, reward in zip(prompts, completions, rewards)
25-
]
26-
},
27-
step=step,
28-
)
91+
},
92+
step=int(state.global_step),
93+
)
2994

3095

31-
trainer = GRPOTrainer(
32-
model="Qwen/Qwen2.5-0.5B",
33-
reward_funcs=[],
34-
args=GRPOConfig(output_dir="out", report_to="trackio"),
35-
train_dataset=[],
36-
)
96+
for run_idx in range(2):
97+
run_name = f"trl-run-{run_idx}"
98+
trackio.init(project=PROJECT_NAME, name=run_name)
99+
100+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
101+
102+
trainer = SFTTrainer(
103+
model=model,
104+
args=SFTConfig(
105+
output_dir=f"./trl_trace_output_{PROJECT_ID}_{run_idx}",
106+
per_device_train_batch_size=2,
107+
max_steps=5,
108+
logging_steps=1,
109+
save_strategy="no",
110+
report_to="none",
111+
learning_rate=5e-5,
112+
dataset_text_field="text",
113+
max_length=64,
114+
),
115+
train_dataset=dataset,
116+
processing_class=tokenizer,
117+
callbacks=[TraceLoggingCallback(examples, run_name)],
118+
)
37119

38-
# Wire `log_rollouts(...)` into your callback or reward loop.
39-
# trainer.train()
120+
trainer.train()
121+
trackio.finish()

tests/unit/test_trace.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ def test_trace_logging_and_query(temp_dir):
7272
assert len(searched) == 1
7373
assert searched[0]["metadata"]["model_version"] == "step-2150"
7474

75-
filtered = SQLiteStorage.get_traces(
76-
"proj", "trace-run", model_version="step-2000"
77-
)
75+
filtered = SQLiteStorage.get_traces("proj", "trace-run", model_version="step-2000")
7876
assert len(filtered) == 1
7977
assert filtered[0]["messages"][2]["content"] == "Sydney."

trackio/frontend/src/App.svelte

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@
8888
let spaceId = $state(null);
8989
let availableSystemDevices = $state([]);
9090
let selectedSystemDevices = $state([]);
91-
let traceModel = $state("All models");
92-
let traceModelChoices = $state(["All models"]);
9391
9492
function runKey(run) {
9593
return run?.id ?? run?.name;
@@ -410,8 +408,6 @@
410408
{metricColumns}
411409
{availableSystemDevices}
412410
bind:selectedSystemDevices
413-
bind:traceModel
414-
{traceModelChoices}
415411
{spaceId}
416412
{logoUrls}
417413
{darkMode}
@@ -444,8 +440,6 @@
444440
<Traces
445441
project={selectedProject}
446442
selectedRuns={selectedRunRecords}
447-
bind:traceModel
448-
bind:traceModelChoices
449443
/>
450444
{:else if currentPage === "system"}
451445
<SystemMetrics

trackio/frontend/src/components/Sidebar.svelte

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
selectedRuns = $bindable([]),
1919
availableSystemDevices = [],
2020
selectedSystemDevices = $bindable([]),
21-
traceModel = $bindable("All models"),
22-
traceModelChoices = [],
2321
smoothing = $bindable(10),
2422
xAxis = $bindable("step"),
2523
logScaleX = $bindable(false),
@@ -413,19 +411,6 @@
413411
{/if}
414412
</div>
415413
416-
{#if currentPage === "traces"}
417-
<span class="section-label">Trace Filters</span>
418-
419-
<div class="section">
420-
<Dropdown
421-
label="Model Version"
422-
choices={traceModelChoices}
423-
bind:value={traceModel}
424-
filterable={false}
425-
/>
426-
</div>
427-
{/if}
428-
429414
{#if currentPage === "metrics" || currentPage === "system"}
430415
<span class="section-label">Display Settings</span>
431416

0 commit comments

Comments
 (0)