feat(m7-m8): evaluation harness, privacy self-test, localisation and release
M7: corpus manifest loader/validation, chrF/BLEU/token/numeric metrics, raw-output and report.md/report.json writer, and sprachmittler-cli languages/detect/eval. A synthetic 4-segment run with the 7B validated the harness; the private-corpus and 1.8B evaluation and the default-model ADR remain open. M8: privacy-check root fixture + seeded self-test wired into make verify; English/German localisation for the app shell; end-user USER_GUIDE; release.sh (unsigned archive, Developer ID sign, notarise/staple, DMG, --dry-run); idle CPU/memory and warm 7B measurements in PERFORMANCE. Docs: PROGRESS M7–M8, MODEL_EVALUATION results log/gate status, PERFORMANCE measurements, TESTING evaluation cases, ADR-008 status. Compatibility/VoiceOver/notarisation gates remain manual. Verified: make verify green (format, strict lint, privacy self-test, Debug+Release signatures, 227 package tests); CLI languages/detect/eval run; release dry-run.
This commit is contained in:
1 parent
a78572adc3
commit
0ab5ae78bf
26 files changed
+1799
-28
No files matched your search
@@ -0,0 +1,321 @@
|
||||
import Foundation
|
||||
import SprachmittlerCore
|
||||
import SprachmittlerEngines
|
||||
|
||||
/// Fixed environment facts for a report.
|
||||
public struct EvaluationEnvironment: Codable, Sendable, Equatable {
|
||||
/// Ollama server version.
|
||||
public var serverVersion: String
|
||||
/// Ollama executable version, when known.
|
||||
public var ollamaVersion: String
|
||||
/// Hardware description.
|
||||
public var hardware: String
|
||||
/// macOS version.
|
||||
public var macOS: String
|
||||
/// Model tags and digests.
|
||||
public var models: [String: String]
|
||||
|
||||
/// Creates an environment block.
|
||||
public init(
|
||||
serverVersion: String,
|
||||
ollamaVersion: String,
|
||||
hardware: String,
|
||||
macOS: String,
|
||||
models: [String: String]
|
||||
) {
|
||||
self.serverVersion = serverVersion
|
||||
self.ollamaVersion = ollamaVersion
|
||||
self.hardware = hardware
|
||||
self.macOS = macOS
|
||||
self.models = models
|
||||
}
|
||||
}
|
||||
|
||||
/// The settings for one evaluation run.
|
||||
public struct EvaluationConfiguration: Sendable, Equatable {
|
||||
/// Model tags to evaluate.
|
||||
public var models: [String]
|
||||
/// Number of repeated runs per segment.
|
||||
public var runs: Int
|
||||
/// Keep-alive for the evaluation requests.
|
||||
public var keepAlive: String
|
||||
/// Sampling values.
|
||||
public var sampling: SamplingOptions
|
||||
/// Output directory.
|
||||
public var outputDirectory: URL
|
||||
|
||||
/// Creates a configuration.
|
||||
public init(
|
||||
models: [String],
|
||||
runs: Int = 3,
|
||||
keepAlive: String = "30m",
|
||||
sampling: SamplingOptions = .publisherDefault,
|
||||
outputDirectory: URL
|
||||
) {
|
||||
self.models = models
|
||||
self.runs = max(1, runs)
|
||||
self.keepAlive = keepAlive
|
||||
self.sampling = sampling
|
||||
self.outputDirectory = outputDirectory
|
||||
}
|
||||
}
|
||||
|
||||
/// One segment/run measurement. It contains no source or translation text.
|
||||
public struct SegmentResult: Codable, Sendable, Equatable {
|
||||
/// Segment ID.
|
||||
public let id: String
|
||||
/// Category.
|
||||
public let category: EvaluationCategory
|
||||
/// Run index.
|
||||
public let run: Int
|
||||
/// chrF percentage.
|
||||
public let chrF: Double
|
||||
/// BLEU percentage.
|
||||
public let bleu: Double
|
||||
/// Protected-token retention.
|
||||
public let tokenRetention: Double
|
||||
/// Numeric consistency.
|
||||
public let numericConsistency: Double
|
||||
/// True for empty, blocked, or transport failure.
|
||||
public let failed: Bool
|
||||
/// Warning/error codes only.
|
||||
public let warnings: [String]
|
||||
/// First-token latency.
|
||||
public let firstTokenMs: Int?
|
||||
/// Total latency.
|
||||
public let totalMs: Int?
|
||||
/// Output character count.
|
||||
public let outputCharacters: Int
|
||||
}
|
||||
|
||||
/// Aggregate metrics for one model/category.
|
||||
public struct CategorySummary: Codable, Sendable, Equatable {
|
||||
/// Category.
|
||||
public let category: EvaluationCategory
|
||||
/// Number of segment runs.
|
||||
public let segments: Int
|
||||
/// Failure rate percentage.
|
||||
public let failureRate: Double
|
||||
/// Mean chrF.
|
||||
public let chrF: Double
|
||||
/// Mean BLEU.
|
||||
public let bleu: Double
|
||||
/// Mean token retention.
|
||||
public let tokenRetention: Double
|
||||
/// Mean numeric consistency.
|
||||
public let numericConsistency: Double
|
||||
/// First-token p50/p90.
|
||||
public let firstTokenP50: Int?
|
||||
public let firstTokenP90: Int?
|
||||
/// Warm total p50/p90.
|
||||
public let totalP50: Int?
|
||||
public let totalP90: Int?
|
||||
}
|
||||
|
||||
/// All results for one model.
|
||||
public struct ModelRunResult: Codable, Sendable, Equatable {
|
||||
/// Model tag.
|
||||
public let model: String
|
||||
/// Temperature.
|
||||
public let temperature: Double
|
||||
/// Number of repeats.
|
||||
public let runs: Int
|
||||
/// Raw segment results.
|
||||
public let segments: [SegmentResult]
|
||||
/// Category aggregates.
|
||||
public let categories: [CategorySummary]
|
||||
}
|
||||
|
||||
/// The complete evaluation report.
|
||||
public struct EvaluationReport: Codable, Sendable, Equatable {
|
||||
/// Environment facts.
|
||||
public let environment: EvaluationEnvironment
|
||||
/// Creation date.
|
||||
public let generatedAt: Date
|
||||
/// The model results.
|
||||
public let models: [ModelRunResult]
|
||||
}
|
||||
|
||||
/// Runs the corpus through the documented pipeline.
|
||||
public struct EvaluationRunner: Sendable {
|
||||
/// Builds a streaming pipeline for a model tag.
|
||||
public typealias PipelineFactory = @Sendable (String) throws -> any TranslationStreaming
|
||||
/// Warms a model with fixed synthetic text.
|
||||
public typealias WarmUp = @Sendable (String) async throws -> Void
|
||||
/// Unloads a model.
|
||||
public typealias Unload = @Sendable (String) async throws -> Void
|
||||
|
||||
private let manifest: ResolvedManifest
|
||||
private let configuration: EvaluationConfiguration
|
||||
private let environment: EvaluationEnvironment
|
||||
private let pipelineFactory: PipelineFactory
|
||||
private let warmUp: WarmUp
|
||||
private let unload: Unload
|
||||
|
||||
/// Creates a runner.
|
||||
public init(
|
||||
manifest: ResolvedManifest,
|
||||
configuration: EvaluationConfiguration,
|
||||
environment: EvaluationEnvironment,
|
||||
pipelineFactory: @escaping PipelineFactory,
|
||||
warmUp: @escaping WarmUp,
|
||||
unload: @escaping Unload
|
||||
) {
|
||||
self.manifest = manifest
|
||||
self.configuration = configuration
|
||||
self.environment = environment
|
||||
self.pipelineFactory = pipelineFactory
|
||||
self.warmUp = warmUp
|
||||
self.unload = unload
|
||||
}
|
||||
|
||||
/// Runs every configured model and writes raw outputs plus the report.
|
||||
public func run() async throws -> EvaluationReport {
|
||||
try FileManager.default.createDirectory(
|
||||
at: configuration.outputDirectory,
|
||||
withIntermediateDirectories: true
|
||||
)
|
||||
var modelResults: [ModelRunResult] = []
|
||||
for model in configuration.models {
|
||||
try await unload(model)
|
||||
try await warmUp(model)
|
||||
var results: [SegmentResult] = []
|
||||
let modelDirectory = configuration.outputDirectory.appendingPathComponent(slug(model))
|
||||
try FileManager.default.createDirectory(at: modelDirectory, withIntermediateDirectories: true)
|
||||
|
||||
for segment in manifest.segments {
|
||||
for run in 0 ..< configuration.runs {
|
||||
let result = await evaluate(segment: segment, run: run, model: model, directory: modelDirectory)
|
||||
results.append(result)
|
||||
}
|
||||
}
|
||||
modelResults.append(
|
||||
ModelRunResult(
|
||||
model: model,
|
||||
temperature: configuration.sampling.temperature,
|
||||
runs: configuration.runs,
|
||||
segments: results,
|
||||
categories: summarize(results)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
let report = EvaluationReport(
|
||||
environment: environment,
|
||||
generatedAt: Date(),
|
||||
models: modelResults
|
||||
)
|
||||
try EvaluationReportWriter.write(report, to: configuration.outputDirectory)
|
||||
return report
|
||||
}
|
||||
|
||||
private func evaluate(
|
||||
segment: ResolvedSegment,
|
||||
run: Int,
|
||||
model: String,
|
||||
directory: URL
|
||||
) async -> SegmentResult {
|
||||
do {
|
||||
let target = HyMT3Languages.resolve(segment.segment.targetLanguage)
|
||||
?? HyMT3Languages.english
|
||||
let pipeline = try pipelineFactory(model)
|
||||
let request = TranslationSessionRequest(
|
||||
captured: CapturedText(
|
||||
text: segment.sourceText,
|
||||
capturedAt: Date(),
|
||||
source: .menu
|
||||
),
|
||||
target: target,
|
||||
model: model,
|
||||
keepAlive: configuration.keepAlive,
|
||||
sampling: configuration.sampling
|
||||
)
|
||||
let result = try await pipeline.translate(request, onDelta: nil)
|
||||
let output = result.translation
|
||||
let failed = output.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
|
||||
|| result.warnings.contains(where: \.blocksReplacement)
|
||||
let file = directory.appendingPathComponent("\(segment.segment.id)-run\(run).txt")
|
||||
try? Data(output.utf8).write(to: file, options: .atomic)
|
||||
return SegmentResult(
|
||||
id: segment.segment.id,
|
||||
category: segment.segment.category,
|
||||
run: run,
|
||||
chrF: EvaluationMetrics.chrF(reference: segment.referenceText, hypothesis: output),
|
||||
bleu: EvaluationMetrics.bleu(reference: segment.referenceText, hypothesis: output),
|
||||
tokenRetention: EvaluationMetrics.tokenRetention(
|
||||
source: segment.sourceText,
|
||||
translation: output
|
||||
),
|
||||
numericConsistency: EvaluationMetrics.numericConsistency(
|
||||
source: segment.sourceText,
|
||||
translation: output
|
||||
),
|
||||
failed: failed,
|
||||
warnings: result.warnings.map(\.code),
|
||||
firstTokenMs: result.timing.firstTokenMs,
|
||||
totalMs: result.timing.totalMs,
|
||||
outputCharacters: output.count
|
||||
)
|
||||
} catch let error as SprachmittlerError {
|
||||
return failure(segment: segment, run: run, code: error.diagnosticCode)
|
||||
} catch {
|
||||
return failure(segment: segment, run: run, code: "transport")
|
||||
}
|
||||
}
|
||||
|
||||
private func failure(segment: ResolvedSegment, run: Int, code: String) -> SegmentResult {
|
||||
SegmentResult(
|
||||
id: segment.segment.id,
|
||||
category: segment.segment.category,
|
||||
run: run,
|
||||
chrF: 0,
|
||||
bleu: 0,
|
||||
tokenRetention: 0,
|
||||
numericConsistency: 0,
|
||||
failed: true,
|
||||
warnings: [code],
|
||||
firstTokenMs: nil,
|
||||
totalMs: nil,
|
||||
outputCharacters: 0
|
||||
)
|
||||
}
|
||||
|
||||
private func summarize(_ results: [SegmentResult]) -> [CategorySummary] {
|
||||
EvaluationCategory.allCases.compactMap { category in
|
||||
let values = results.filter { $0.category == category }
|
||||
guard !values.isEmpty else { return nil }
|
||||
let first = values.compactMap(\.firstTokenMs)
|
||||
let totals = values.compactMap(\.totalMs)
|
||||
return CategorySummary(
|
||||
category: category,
|
||||
segments: values.count,
|
||||
failureRate: 100 * Double(values.filter(\.failed).count) / Double(values.count),
|
||||
chrF: mean(values.map(\.chrF)),
|
||||
bleu: mean(values.map(\.bleu)),
|
||||
tokenRetention: mean(values.map(\.tokenRetention)),
|
||||
numericConsistency: mean(values.map(\.numericConsistency)),
|
||||
firstTokenP50: percentile(first, 0.5),
|
||||
firstTokenP90: percentile(first, 0.9),
|
||||
totalP50: percentile(totals, 0.5),
|
||||
totalP90: percentile(totals, 0.9)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private func mean(_ values: [Double]) -> Double {
|
||||
guard !values.isEmpty else { return 0 }
|
||||
return values.reduce(0, +) / Double(values.count)
|
||||
}
|
||||
|
||||
private func percentile(_ values: [Int], _ fraction: Double) -> Int? {
|
||||
guard !values.isEmpty else { return nil }
|
||||
let sorted = values.sorted()
|
||||
let index = min(sorted.count - 1, Int((Double(sorted.count - 1) * fraction).rounded()))
|
||||
return sorted[index]
|
||||
}
|
||||
|
||||
private func slug(_ model: String) -> String {
|
||||
model.replacingOccurrences(of: "/", with: "_")
|
||||
.replacingOccurrences(of: ":", with: "_")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user