vanishingradient commited on
Commit
d5f40a1
·
verified ·
1 Parent(s): 8b908da

Fix Langfuse ckpt/heldout child trace_context

Browse files
Files changed (1) hide show
  1. kaggle/train_grpo.py +19 -5
kaggle/train_grpo.py CHANGED
@@ -344,12 +344,20 @@ def run_heldout_agent_eval(
344
  gen_ids = out_ids[0][inputs_local["input_ids"].shape[-1] :]
345
  return tokenizer.decode(gen_ids, skip_special_tokens=True)
346
 
347
- if lf is not None and getattr(env, "_root", None) is not None:
 
 
 
348
  with lf.start_as_current_observation(
349
  name=f"llm-turn-{turn}",
350
  as_type="generation",
 
351
  input={"messages_tail": messages[-2:], "turn": turn},
352
- metadata={"phase": "ckpt_eval", "turn": turn},
 
 
 
 
353
  ) as gen_span:
354
  completion = _generate_once()
355
  calls = _parse_tool_calls(completion)
@@ -486,12 +494,10 @@ def langfuse_log_ckpt_eval(
486
  n_perfect = sum(1 for r in results if float(r.get("pass_frac") or 0) >= 1.0)
487
  n_resolved = sum(1 for r in results if r.get("resolved"))
488
 
489
- # Own root trace (do not nest under / swallow per-rollout traces).
490
- agg_trace_id = lf.create_trace_id()
491
  with lf.start_as_current_observation(
492
  name=f"kaggle-ckpt-eval:step-{step}",
493
  as_type="evaluator",
494
- trace_context={"trace_id": agg_trace_id},
495
  metadata={
496
  "step": step,
497
  "hf_revision": hf_revision,
@@ -503,10 +509,18 @@ def langfuse_log_ckpt_eval(
503
  input={"step": step, "hf_revision": hf_revision, "n": n},
504
  end_on_exit=True,
505
  ) as root:
 
 
506
  for r in results:
 
 
 
 
 
507
  with lf.start_as_current_observation(
508
  name=f"ckpt-summary:{r['task_id']}",
509
  as_type="span",
 
510
  input={"prompt": str(r.get("prompt", ""))[:2000]},
511
  ) as span:
512
  span.update(
 
344
  gen_ids = out_ids[0][inputs_local["input_ids"].shape[-1] :]
345
  return tokenizer.decode(gen_ids, skip_special_tokens=True)
346
 
347
+ child_ctx = None
348
+ if hasattr(env, "_child_trace_context"):
349
+ child_ctx = env._child_trace_context()
350
+ if lf is not None and child_ctx is not None:
351
  with lf.start_as_current_observation(
352
  name=f"llm-turn-{turn}",
353
  as_type="generation",
354
+ trace_context=child_ctx,
355
  input={"messages_tail": messages[-2:], "turn": turn},
356
+ metadata={
357
+ "phase": "ckpt_eval",
358
+ "turn": turn,
359
+ "trace_id": env._trace_id,
360
+ },
361
  ) as gen_span:
362
  completion = _generate_once()
363
  calls = _parse_tool_calls(completion)
 
494
  n_perfect = sum(1 for r in results if float(r.get("pass_frac") or 0) >= 1.0)
495
  n_resolved = sum(1 for r in results if r.get("resolved"))
496
 
497
+ # Clean root (no bare trace_context — that creates phantom orphan parents).
 
498
  with lf.start_as_current_observation(
499
  name=f"kaggle-ckpt-eval:step-{step}",
500
  as_type="evaluator",
 
501
  metadata={
502
  "step": step,
503
  "hf_revision": hf_revision,
 
509
  input={"step": step, "hf_revision": hf_revision, "n": n},
510
  end_on_exit=True,
511
  ) as root:
512
+ agg_trace_id = str(getattr(root, "trace_id", "") or "")
513
+ root_span_id = str(getattr(root, "id", "") or "")
514
  for r in results:
515
+ child_ctx = (
516
+ {"trace_id": agg_trace_id, "parent_span_id": root_span_id}
517
+ if agg_trace_id and root_span_id
518
+ else None
519
+ )
520
  with lf.start_as_current_observation(
521
  name=f"ckpt-summary:{r['task_id']}",
522
  as_type="span",
523
+ trace_context=child_ctx,
524
  input={"prompt": str(r.get("prompt", ""))[:2000]},
525
  ) as span:
526
  span.update(