|
| 1 | +using System.Text.Json; |
| 2 | + |
| 3 | +namespace BitNetSharp.Core; |
| 4 | + |
| 5 | +public sealed record TraditionalLocalCheckpointValidationResult( |
| 6 | + string Prompt, |
| 7 | + string OriginalResponse, |
| 8 | + string ReloadedResponse, |
| 9 | + bool ResponsesMatch); |
| 10 | + |
| 11 | +internal sealed record TraditionalLocalCheckpointDocument( |
| 12 | + string Format, |
| 13 | + string ModelId, |
| 14 | + int Seed, |
| 15 | + int EmbeddingDimension, |
| 16 | + int ContextWindow, |
| 17 | + IReadOnlyList<string> Vocabulary, |
| 18 | + float[] TokenEmbeddings, |
| 19 | + float[] OutputWeights, |
| 20 | + float[] OutputBias, |
| 21 | + int MaxResponseTokens, |
| 22 | + string PrimaryLanguage); |
| 23 | + |
| 24 | +public static class TraditionalLocalCheckpoint |
| 25 | +{ |
| 26 | + private const string FormatName = "traditional-local.repository-checkpoint.v1"; |
| 27 | + |
| 28 | + public static void Save(TraditionalLocalModel model, string path) |
| 29 | + { |
| 30 | + ArgumentNullException.ThrowIfNull(model); |
| 31 | + ArgumentException.ThrowIfNullOrWhiteSpace(path); |
| 32 | + |
| 33 | + var directory = Path.GetDirectoryName(path); |
| 34 | + if (!string.IsNullOrWhiteSpace(directory)) |
| 35 | + { |
| 36 | + Directory.CreateDirectory(directory); |
| 37 | + } |
| 38 | + |
| 39 | + var document = new TraditionalLocalCheckpointDocument( |
| 40 | + FormatName, |
| 41 | + model.ModelId, |
| 42 | + model.Seed, |
| 43 | + model.EmbeddingDimension, |
| 44 | + model.ContextWindow, |
| 45 | + model.Options.Vocabulary.ToArray(), |
| 46 | + model.ExportTokenEmbeddings(), |
| 47 | + model.ExportOutputWeights(), |
| 48 | + model.ExportOutputBias(), |
| 49 | + model.Options.MaxResponseTokens, |
| 50 | + model.Options.PrimaryLanguage); |
| 51 | + File.WriteAllText(path, JsonSerializer.Serialize(document, new JsonSerializerOptions { WriteIndented = true })); |
| 52 | + } |
| 53 | + |
| 54 | + public static TraditionalLocalModel Load(string path, VerbosityLevel verbosity = VerbosityLevel.Normal) |
| 55 | + { |
| 56 | + ArgumentException.ThrowIfNullOrWhiteSpace(path); |
| 57 | + |
| 58 | + var document = JsonSerializer.Deserialize<TraditionalLocalCheckpointDocument>(File.ReadAllText(path)) |
| 59 | + ?? throw new InvalidOperationException("Could not deserialize the traditional local checkpoint document."); |
| 60 | + if (!string.Equals(document.Format, FormatName, StringComparison.Ordinal)) |
| 61 | + { |
| 62 | + throw new InvalidOperationException($"Unsupported checkpoint format '{document.Format}'."); |
| 63 | + } |
| 64 | + |
| 65 | + var model = new TraditionalLocalModel( |
| 66 | + new BitNetOptions( |
| 67 | + document.Vocabulary.ToArray(), |
| 68 | + verbosity, |
| 69 | + document.MaxResponseTokens, |
| 70 | + document.PrimaryLanguage), |
| 71 | + document.EmbeddingDimension, |
| 72 | + document.ContextWindow, |
| 73 | + document.Seed); |
| 74 | + model.ImportState(document.TokenEmbeddings, document.OutputWeights, document.OutputBias); |
| 75 | + return model; |
| 76 | + } |
| 77 | + |
| 78 | + public static TraditionalLocalCheckpointValidationResult ValidateRoundTrip(TraditionalLocalModel model, string prompt) |
| 79 | + { |
| 80 | + ArgumentNullException.ThrowIfNull(model); |
| 81 | + ArgumentException.ThrowIfNullOrWhiteSpace(prompt); |
| 82 | + |
| 83 | + var checkpointPath = Path.Combine(Path.GetTempPath(), $"traditional-local-checkpoint-{Guid.NewGuid():N}.json"); |
| 84 | + try |
| 85 | + { |
| 86 | + Save(model, checkpointPath); |
| 87 | + var reloaded = Load(checkpointPath, model.Options.Verbosity); |
| 88 | + var original = model.GenerateResponse(prompt, maxTokens: 4); |
| 89 | + var roundTripped = reloaded.GenerateResponse(prompt, maxTokens: 4); |
| 90 | + return new TraditionalLocalCheckpointValidationResult( |
| 91 | + prompt, |
| 92 | + original.ResponseText, |
| 93 | + roundTripped.ResponseText, |
| 94 | + string.Equals(original.ResponseText, roundTripped.ResponseText, StringComparison.Ordinal)); |
| 95 | + } |
| 96 | + finally |
| 97 | + { |
| 98 | + if (File.Exists(checkpointPath)) |
| 99 | + { |
| 100 | + File.Delete(checkpointPath); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments