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.
190 lines
6.8 KiB
Swift
190 lines
6.8 KiB
Swift
import ArgumentParser
|
|
import Foundation
|
|
import SprachmittlerCore
|
|
import SprachmittlerEngines
|
|
import SprachmittlerEval
|
|
import SprachmittlerOllama
|
|
|
|
struct EvalCommand: AsyncParsableCommand {
|
|
static let configuration = CommandConfiguration(
|
|
commandName: "eval",
|
|
abstract: "Run the private-corpus evaluation and write report.md/report.json."
|
|
)
|
|
|
|
@Option(name: .long, help: "Corpus manifest JSON.")
|
|
var manifest: String
|
|
|
|
@Option(name: .long, help: "Comma-separated model tags.")
|
|
var models = HyMT3ModelPreset.defaultModel
|
|
|
|
@Option(name: .long, help: "Target BCP-47 code.")
|
|
var target = "de"
|
|
|
|
@Option(name: .long, help: "Runs per segment (default 3).")
|
|
var runs = 3
|
|
|
|
@Option(name: .long, help: "Sampling temperature override.")
|
|
var temperature: Double?
|
|
|
|
@Option(name: .long, help: "Ollama keep-alive during the run.")
|
|
var keepAlive = HyMT3ModelPreset.defaultKeepAlive
|
|
|
|
@Option(name: .long, help: "Output directory for raw outputs and reports.")
|
|
var out: String?
|
|
|
|
@OptionGroup var endpoint: EndpointOptions
|
|
|
|
func validate() throws {
|
|
guard (1 ... 5).contains(runs) else {
|
|
throw ValidationError("--runs must be between 1 and 5.")
|
|
}
|
|
if let temperature, !(0 ... 2).contains(temperature) {
|
|
throw ValidationError("--temperature must be between 0 and 2.")
|
|
}
|
|
guard HyMT3Languages.resolve(target) != nil else {
|
|
throw ValidationError("Unknown target language '\(target)'.")
|
|
}
|
|
guard !modelTags().isEmpty else {
|
|
throw ValidationError("Pass at least one model tag.")
|
|
}
|
|
}
|
|
|
|
func run() async throws {
|
|
try await CLIError.perform {
|
|
try await execute()
|
|
}
|
|
}
|
|
|
|
private func execute() async throws {
|
|
let manifestURL = URL(fileURLWithPath: manifest)
|
|
guard let language = HyMT3Languages.resolve(target) else {
|
|
throw ValidationError("Unknown target language '\(target)'.")
|
|
}
|
|
let resolved = try EvaluationManifestLoader.load(manifestURL: manifestURL, target: language.code)
|
|
let environment = try endpoint.makeEnvironment()
|
|
let settings = makeSettings(environment: environment, target: language)
|
|
let configuration = EvaluationConfiguration(
|
|
models: modelTags(),
|
|
runs: runs,
|
|
keepAlive: keepAlive,
|
|
sampling: settings.samplingOptions,
|
|
outputDirectory: outputDirectory()
|
|
)
|
|
let evaluationEnvironment = try await makeEnvironmentBlock(environment, settings: settings)
|
|
let evaluation = makeRunner(
|
|
resolved: resolved,
|
|
configuration: configuration,
|
|
environment: evaluationEnvironment,
|
|
settings: settings,
|
|
endpointEnvironment: environment
|
|
)
|
|
let report = try await evaluation.run()
|
|
printSummary(report, directory: configuration.outputDirectory)
|
|
}
|
|
|
|
private func makeSettings(environment: CLIEnvironment, target: ModelLanguage) -> AppSettings {
|
|
var draft = AppSettings()
|
|
draft.endpoint = environment.baseURL
|
|
draft.allowRemoteEndpoint = environment.allowRemote
|
|
draft.targetLanguage = target.code
|
|
draft.model = modelTags().first ?? HyMT3ModelPreset.defaultModel
|
|
draft.warmUpOnLaunch = false
|
|
draft.keepModelLoaded = keepAlive
|
|
if let temperature {
|
|
draft.temperature = temperature
|
|
}
|
|
return draft
|
|
}
|
|
|
|
private func makeEnvironmentBlock(
|
|
_ environment: CLIEnvironment,
|
|
settings: AppSettings
|
|
) async throws -> EvaluationEnvironment {
|
|
let catalog = try environment.makeCatalog()
|
|
let version = try await catalog.serverVersion()
|
|
let installed = try? await catalog.installedModels()
|
|
var digests: [String: String] = [:]
|
|
for model in installed ?? [] {
|
|
digests[model.name] = String(model.digest.prefix(12))
|
|
}
|
|
return EvaluationEnvironment(
|
|
serverVersion: version,
|
|
ollamaVersion: version,
|
|
hardware: hardware(),
|
|
macOS: ProcessInfo.processInfo.operatingSystemVersionString,
|
|
models: digests
|
|
)
|
|
}
|
|
|
|
private func makeRunner(
|
|
resolved: ResolvedManifest,
|
|
configuration: EvaluationConfiguration,
|
|
environment: EvaluationEnvironment,
|
|
settings: AppSettings,
|
|
endpointEnvironment: CLIEnvironment
|
|
) -> EvaluationRunner {
|
|
let registry = AdapterRegistry(adapters: [HyMT3Adapter()])
|
|
return EvaluationRunner(
|
|
manifest: resolved,
|
|
configuration: configuration,
|
|
environment: environment,
|
|
pipelineFactory: { model in
|
|
var snapshot = settings
|
|
snapshot.model = model
|
|
return try TranslationPipeline(
|
|
chat: endpointEnvironment.makeChatClient(),
|
|
registry: registry,
|
|
chunker: snapshot.chunker
|
|
)
|
|
},
|
|
warmUp: { model in
|
|
let chat = try endpointEnvironment.makeChatClient()
|
|
try await OllamaWarmUpRunner(chat: chat).run(
|
|
HyMT3Adapter().warmUpRequest(model: model, keepAlive: keepAlive)
|
|
)
|
|
},
|
|
unload: { model in
|
|
let chat = try endpointEnvironment.makeChatClient()
|
|
try await OllamaWarmUpRunner(chat: chat).run(
|
|
HyMT3Adapter().unloadRequest(model: model)
|
|
)
|
|
}
|
|
)
|
|
}
|
|
|
|
private func printSummary(_ report: EvaluationReport, directory: URL) {
|
|
print("Report: \(directory.path)")
|
|
for model in report.models {
|
|
for category in model.categories {
|
|
print(
|
|
"\(model.model)\t\(category.category.rawValue)\t"
|
|
+ String(
|
|
format: "fail %.1f%%\tchrF %.2f\tBLEU %.2f",
|
|
category.failureRate,
|
|
category.chrF,
|
|
category.bleu
|
|
)
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func modelTags() -> [String] {
|
|
models.split(separator: ",").map { $0.trimmingCharacters(in: .whitespaces) }.filter { !$0.isEmpty }
|
|
}
|
|
|
|
private func outputDirectory() -> URL {
|
|
if let out {
|
|
return URL(fileURLWithPath: out)
|
|
}
|
|
let formatter = DateFormatter()
|
|
formatter.dateFormat = "yyyy-MM-dd"
|
|
return URL(fileURLWithPath: "evaluation/results/\(formatter.string(from: Date()))")
|
|
}
|
|
|
|
private func hardware() -> String {
|
|
let memory = ProcessInfo.processInfo.physicalMemory / 1_073_741_824
|
|
return "Apple Silicon \(memory) GB, \(ProcessInfo.processInfo.processorCount) cores"
|
|
}
|
|
}
|