Repository navigation
Report held-out evaluation during training #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jeffhandley
merged 15 commits into
main
from
matouskozak-held-out-evaluation-foundation
Oct 24, 2026
Merged
Changes from 5 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e605f0b
Add saved held-out model evaluation
matouskozak 65969f3
Keep held-out foundation scope minimal
matouskozak b6a5ecf
Use the simpler frozen holdout evaluation
matouskozak 8f82525
Keep held-out evaluation details out of README
matouskozak b354513
Reuse dataset caches with a fixed train-test split
matouskozak 14b3ddb
Consolidate held-out scoring into Tester
matouskozak 8b7b731
Evaluate the holdout during training and keep Test historical
matouskozak 7e8fe41
Report coverage at held-out precision targets
matouskozak e6602cb
Shorten the historical test warning
matouskozak 694aec5
Preserve the original Trainer entry point
matouskozak 82acf5f
Limit trainer tests to critical paths
matouskozak d4a2e16
Keep action descriptions concise and remove README expansion
matouskozak ce8eeb7
Keep trainer tests focused on behavior
matouskozak 1baf249
Run trainer tests in parallel
matouskozak ac3443d
Move held-out evaluation types to their own file
matouskozak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.IO.Compression; | ||
| using System.Security.Cryptography; | ||
| using System.Text; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| public sealed class DatasetSplit | ||
| { | ||
| private const string ModelReferenceEntry = "DatasetSplit.sha256"; | ||
| private static readonly JsonSerializerOptions JsonOptions = new() | ||
| { | ||
| PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
| UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow, | ||
| RespectNullableAnnotations = true | ||
| }; | ||
| private string fingerprint = string.Empty; | ||
|
|
||
| [JsonRequired] | ||
| public int SchemaVersion { get; init; } = 1; | ||
| public required string DatasetSha256 { get; init; } | ||
| [JsonRequired] | ||
| public int ColumnCount { get; init; } | ||
| [JsonRequired] | ||
| public int RowCount { get; init; } | ||
| public required int[] TestRows { get; init; } | ||
|
|
||
| public static string GetPath(string dataPath) => Path.ChangeExtension(dataPath, ".split.json"); | ||
|
|
||
| public static DatasetSplit Create(string dataPath) | ||
| { | ||
| int columnCount = ReadHeader(dataPath).Length; | ||
| int rowCount = ReadRecords(dataPath).Count(); | ||
| int testCount = rowCount < 2 ? 0 : Math.Clamp((int)Math.Round(rowCount * 0.2), 1, rowCount - 1); | ||
| int[] indices = [.. Enumerable.Range(0, rowCount)]; | ||
| new Random(42).Shuffle(indices); | ||
| var split = new DatasetSplit | ||
| { | ||
| DatasetSha256 = HashFile(dataPath), | ||
| ColumnCount = columnCount, | ||
| RowCount = rowCount, | ||
| TestRows = [.. indices.Take(testCount).Order()] | ||
| }; | ||
| byte[] bytes = JsonSerializer.SerializeToUtf8Bytes(split, JsonOptions); | ||
| File.WriteAllBytes(GetPath(dataPath), bytes); | ||
| split.fingerprint = Convert.ToHexStringLower(SHA256.HashData(bytes)); | ||
| return split; | ||
| } | ||
|
|
||
| public static DatasetSplit Load(string dataPath) | ||
| { | ||
| if (!File.Exists(dataPath) || !File.Exists(GetPath(dataPath))) | ||
| { | ||
| throw new FileNotFoundException( | ||
| "The original dataset or its split manifest is unavailable. Download data with a split manifest before training; evaluation requires the original cached dataset.", | ||
| dataPath); | ||
| } | ||
|
|
||
| byte[] bytes = File.ReadAllBytes(GetPath(dataPath)); | ||
| var split = JsonSerializer.Deserialize<DatasetSplit>(bytes, JsonOptions) | ||
| ?? throw new InvalidDataException("The dataset split manifest is empty."); | ||
| if (split.SchemaVersion != 1 || split.ColumnCount is not (3 or 5) || split.RowCount < 0 || | ||
| split.TestRows is null || (split.RowCount > 0 && split.TestRows.Length >= split.RowCount) || | ||
| split.TestRows.Where((row, index) => row < 0 || row >= split.RowCount || | ||
| (index > 0 && row <= split.TestRows[index - 1])).Any()) | ||
| { | ||
| throw new InvalidDataException("The dataset split manifest has invalid schema or row membership."); | ||
| } | ||
|
|
||
| if (!HashFile(dataPath).Equals(split.DatasetSha256, StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| throw new InvalidDataException("The dataset does not match its split manifest. Restore the original dataset; do not regenerate the split for evaluation."); | ||
| } | ||
|
|
||
| if (ReadHeader(dataPath).Length != split.ColumnCount) | ||
| { | ||
| throw new InvalidDataException("The dataset columns do not match its split manifest."); | ||
| } | ||
|
|
||
| split.fingerprint = Convert.ToHexStringLower(SHA256.HashData(bytes)); | ||
| return split; | ||
| } | ||
|
|
||
| public IEnumerable<DatasetRow> ReadRows(string dataPath, bool test) | ||
| { | ||
| var testRows = new HashSet<int>(TestRows); | ||
| int index = 0; | ||
| foreach (string[] fields in ReadRecords(dataPath)) | ||
| { | ||
| if (testRows.Contains(index++) == test) | ||
| { | ||
| DatasetRow row = ColumnCount == 3 ? new DatasetRow() : new PullRequestDatasetRow | ||
| { | ||
| FileNames = fields[3], | ||
| FolderNames = fields[4] | ||
| }; | ||
| row.Label = fields[0]; | ||
| row.Title = fields[1]; | ||
| row.Body = fields[2]; | ||
| yield return row; | ||
| } | ||
| } | ||
|
|
||
| if (index != RowCount) | ||
| { | ||
| throw new InvalidDataException("The dataset row count does not match its split manifest."); | ||
| } | ||
| } | ||
|
|
||
| public void WriteModelReference(string modelPath) | ||
| { | ||
| using var archive = ZipFile.Open(modelPath, ZipArchiveMode.Update); | ||
| archive.GetEntry(ModelReferenceEntry)?.Delete(); | ||
| using var writer = new StreamWriter(archive.CreateEntry(ModelReferenceEntry).Open(), Encoding.ASCII); | ||
| writer.Write(fingerprint); | ||
| } | ||
|
|
||
| public void VerifyModelReference(string modelPath) | ||
| { | ||
| using var archive = ZipFile.OpenRead(modelPath); | ||
| var entry = archive.GetEntry(ModelReferenceEntry) | ||
| ?? throw new InvalidDataException("The model has no dataset split reference. Retrain it using a dataset with a split manifest."); | ||
| using var reader = new StreamReader(entry.Open()); | ||
| if (!fingerprint.Equals(reader.ReadToEnd(), StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| throw new InvalidDataException("The model was trained with a different dataset or split. Restore the original dataset and manifest."); | ||
| } | ||
| } | ||
|
|
||
| private static string HashFile(string path) | ||
| { | ||
| using var stream = File.OpenRead(path); | ||
| return Convert.ToHexStringLower(SHA256.HashData(stream)); | ||
| } | ||
|
|
||
| private static string[] ReadHeader(string path) | ||
| { | ||
| string[] fields = File.ReadLines(path).FirstOrDefault()?.Split('\t') | ||
| ?? throw new InvalidDataException("The dataset has no header."); | ||
| string[] expected = fields.Length == 3 | ||
| ? ["Label", "Title", "Body"] | ||
| : ["Label", "Title", "Body", "FileNames", "FolderNames"]; | ||
| if (!fields.SequenceEqual(expected)) | ||
| { | ||
| throw new InvalidDataException("The dataset has an invalid column header."); | ||
| } | ||
|
|
||
| return fields; | ||
| } | ||
|
|
||
| private static IEnumerable<string[]> ReadRecords(string path) | ||
| { | ||
| int columnCount = ReadHeader(path).Length; | ||
| foreach (string line in File.ReadLines(path).Skip(1)) | ||
| { | ||
| string[] fields = line.Split('\t'); | ||
| if (fields.Length != columnCount) | ||
| { | ||
| throw new InvalidDataException("A dataset row has an invalid column count."); | ||
| } | ||
|
|
||
| // Match TextLoader's whole-line trailing whitespace trim, not a trim of every field. | ||
| for (int last = fields.Length - 1; last >= 0; last--) | ||
| { | ||
| fields[last] = fields[last].TrimEnd(); | ||
| if (fields[last].Length > 0) | ||
| { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| yield return [.. fields.Select(field => field.TrimStart(' '))]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public class DatasetRow | ||
| { | ||
| public string Label { get; set; } = string.Empty; | ||
| public string Title { get; set; } = string.Empty; | ||
| public string Body { get; set; } = string.Empty; | ||
| } | ||
|
|
||
| public sealed class PullRequestDatasetRow : DatasetRow | ||
| { | ||
| public string FileNames { get; set; } = string.Empty; | ||
| public string FolderNames { get; set; } = string.Empty; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.