Handle zero-token source rows in code trace audit

This commit is contained in:
2026-07-23 23:52:03 +08:00
parent fbaa909723
commit 1d9182f305
5 changed files with 50 additions and 6 deletions

View File

@@ -131,6 +131,9 @@ def choose_window(
def scan_source(path: Path, args: argparse.Namespace) -> dict[str, Any]:
rows = 0
source_rows = 0
invalid_zero_token_rows = 0
invalid_zero_token_examples: list[dict[str, Any]] = []
first_timestamp = None
last_timestamp = None
previous_timestamp = None
@@ -145,6 +148,7 @@ def scan_source(path: Path, args: argparse.Namespace) -> dict[str, Any]:
sampling_rows = 0
schema_keys: Counter[str] = Counter()
for line_number, row in iter_jsonl(path):
source_rows += 1
missing = [
key
for key in ("timestamp", "input_length", "output_length")
@@ -153,6 +157,22 @@ def scan_source(path: Path, args: argparse.Namespace) -> dict[str, Any]:
if missing:
raise ValueError(f"{path}:{line_number}: missing required fields {missing}")
timestamp = float(row["timestamp"])
input_tokens = int(row["input_length"])
output_tokens = int(row["output_length"])
schema_keys.update(row.keys())
if input_tokens <= 0 or output_tokens <= 0:
invalid_zero_token_rows += 1
if len(invalid_zero_token_examples) < 20:
invalid_zero_token_examples.append(
{
"line_number": line_number,
"chat_id": row.get("chat_id"),
"timestamp": timestamp,
"input_length": input_tokens,
"output_length": output_tokens,
}
)
continue
if first_timestamp is None:
first_timestamp = timestamp
if previous_timestamp is not None and timestamp < previous_timestamp:
@@ -169,10 +189,6 @@ def scan_source(path: Path, args: argparse.Namespace) -> dict[str, Any]:
max_gaps.get(previous_bin, 0.0),
timestamp - previous_timestamp,
)
input_tokens = int(row["input_length"])
output_tokens = max(1, int(row["output_length"]))
if input_tokens <= 0:
raise ValueError(f"{path}:{line_number}: input_length must be positive")
input_lengths.append(input_tokens)
output_lengths.append(output_tokens)
total_lengths.append(input_tokens + output_tokens)
@@ -186,7 +202,6 @@ def scan_source(path: Path, args: argparse.Namespace) -> dict[str, Any]:
isinstance(row.get("prompt"), (str, list)) and bool(row.get("prompt"))
)
sampling_rows += int("sampling_u" in row)
schema_keys.update(row.keys())
rows += 1
previous_timestamp = timestamp
last_timestamp = timestamp
@@ -205,6 +220,10 @@ def scan_source(path: Path, args: argparse.Namespace) -> dict[str, Any]:
return {
"source": str(path.resolve()),
"rows": rows,
"source_rows": source_rows,
"invalid_zero_token_rows": invalid_zero_token_rows,
"invalid_zero_token_fraction": invalid_zero_token_rows / source_rows,
"invalid_zero_token_examples": invalid_zero_token_examples,
"first_timestamp": first_timestamp,
"last_timestamp": last_timestamp,
"span_s": last_timestamp - first_timestamp,
@@ -253,7 +272,9 @@ def scan_window(source: Path, window: dict[str, Any]) -> dict[str, Any]:
if timestamp >= end:
break
input_tokens = int(row["input_length"])
output_tokens = max(1, int(row["output_length"]))
output_tokens = int(row["output_length"])
if input_tokens <= 0 or output_tokens <= 0:
continue
inputs.append(input_tokens)
outputs.append(output_tokens)
totals.append(input_tokens + output_tokens)